【发布时间】:2008-11-26 17:15:46
【问题描述】:
我正在尝试“交换”两个单元格的内容及其映射。为此,我需要拖放对单元格的引用,而不是字符串值本身。然后我可以使用这个引用来更新字典并获取值。它允许我进行交换,因为我将引用旧单元格以在其中添加所需的值。
我遇到的问题是我不确定如何传递单元格引用:
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
我正在尝试做的事情可能吗?我完全破坏了吗?谢谢:)
【问题讨论】:
-
当我从当前单元格拖动时,它会在鼠标单击时被选中,但是当我想拖放到另一行的其他位置时,目标单元格不会被选中,因此当前单元格不会在我想更新它时保留它的引用。有什么想法吗?
标签: .net vb.net reference drag-and-drop object