1

2 つのセルの内容とそのマッピングを「交換」しようとしています。これを行うには、文字列値自体ではなく、セルへの参照をドラッグ アンド ドロップする必要があります。次に、この参照を使用して 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

そしてドロップイベントで:

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 に答える