Drag & Drop VB.Net Object Reference

I am trying to "swap" the contents of two cells and map them. For this I need to drag the cell reference as opposed to the string value itself. Then I can use this link to update the dictionary as well as get the value. This allows me to swap as I will have a reference to the old cell to add the value I want there.

The problem I'm running into is I'm 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

      

Am I trying to do this? Did I completely break it? Thank:)

+1


source to share


1 answer


Your e.Data

is IDataObject

, not the value sent with DoDragDrop

.

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

.

To fix your code, replace the problem line with the following:



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

      

(or any type DataGridView1.CurrentCell

).

You can get a list of the types available for deletion by calling e.Data.GetFormats()

.

+1


source







All Articles