1

我正在尝试“交换”两个单元格的内容及其映射。为此,我需要拖放对单元格的引用,而不是字符串值本身。然后,我可以使用此引用来更新 Dictionary 并获取值。它允许我进行交换,因为我将引用旧单元格以在其中添加所需的值。

我遇到的问题是我不确定如何传递单元格引用:

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

在 drop 事件中:

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

我正在尝试做的事情可能吗?我完全破坏了吗?谢谢 :)

4

1 回答 1

1

e.Data是一个IDataObject而不是你发送的价值DoDragDrop

要获取您发送的值,您必须调用e.Data.GetData(...).

要修复您的代码,请将问题行替换为:

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

(或任何类型DataGridView1.CurrentCell。)

您可以通过调用获取可删除的类型列表e.Data.GetFormats()

于 2009-03-03T19:09:27.533 回答