2008-11-26 35 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()被丟棄。