2008-11-26 64 views
1

我想'交换'两个单元格的内容及其映射。为此,我需要拖放对单元格的引用,而不是字符串值本身。然后,我可以使用此引用来更新字典并获取该值。它允许我进行交换,因为我将引用旧单元格以在其中添加所需的值。拖放一个对象引用VB.Net

我遇到的问题是我不知道如何通过单元格引用:

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 

,并在下拉事件:

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 

是什么,我试图做可能吗?我完全打破它了吗?谢谢:)

+0

当我从当前单元格拖动时,它会在鼠标单击时被选中,但是当我想要放在另一行的其他位置时,目标单元格不会被选中,因此当前单元格不会保留其引用我想更新它。 有什么想法? – mustafabar 2009-08-29 07:35:04

回答

1

e.DataIDataObjectDoDragDrop发送的值。请致电e.Data.GetData(...)

要解决你的代码,以替换问题行:

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

(或任何的DataGridView1.CurrentCell的类型。)

你可以得到的可用类型列表来调用e.Data.GetFormats()被丢弃。