Drag and Dropping an Object Reference VB.Net

I am trying to 'swap' two cells' contents, and their mappings. To do this, I need to drag and drop a reference to the cell as opposed to the string value itself. I can then use this reference to update a Dictionary as well as get the value. It allows allows me to do the swap as I will have a reference to the old cell to add the value needed in there.

The problem I am having is I am not sure how to pass the cell reference:

Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
    If e.Button = MouseButtons.Left Then
        DataGridView1.DoDragDrop(DataGridView1.CurrentCell, DragDropEffects.Copy)
    End If

End Sub

and in the drop event:

Private Sub DataGridView1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop

   'Problem is here -->'Dim copyedFromCell As DataGridViewCell = DirectCast(e.Data(), DataGridViewCell)** 
    Dim copyedFromKey As String = GetMappingForCell(copyedFromCell) 
    Dim thisKey As String = GetMappingForCell(DataGridView1.CurrentCell)
    Dim copyedFromValue As String = copyedFromCell.Value
    Dim thisValue As String = DataGridView1.CurrentCell.Value

    mappings(copyedFromKey) = DataGridView1.CurrentCell
    mappings(thisKey) = copyedFromCell

    DataGridView1.CurrentCell.Value = copyedFromValue
    copyedFromCell.Value = thisValue

End Sub

Is what I am trying to do possible? Have I completely broken it? Thanks :)

Answers


Your e.Data is a IDataObject, not the value you sent with DoDragDrop.

To get the value you sent, you must call e.Data.GetData(...).

To fix your code, replace the problem line with:

Dim copiedFromCell As DataGridViewCell = _
   e.Data.GetData(GetType(DataGridViewTextBoxCell))

(or whatever the type of DataGridView1.CurrentCell is.)

You can get a list of types available to be dropped by calling e.Data.GetFormats().


Need Your Help

customAdapter for object isnt working

android gridview custom-adapter

I have a model class. Now from my activity i want to set the values in model class & show them in gridview using my custom adapter. After that i need to store the object in a variable(class type)

Why can't CP systems also be CAP?

distributed-computing cap-theorem

My understanding of the CAP acronym is as follows: