我正在尝试“交换”两个单元格的内容及其映射。为此,我需要拖放对单元格的引用,而不是字符串值本身。然后,我可以使用此引用来更新 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
我正在尝试做的事情可能吗?我完全破坏了吗?谢谢 :)