确定DataGridView行是否在Treeview上拖动

时间:2016-02-06 01:52:00

标签: vb.net

如何确定从DataGridView拖动到TreeView的项目实际上是否是DataGridView行?

我有TreeView已经能够拖放到自身,但我想检查TreeView DragOver以检查它是否是TreeNode或DataGridView行。

我知道我可以通过这样做来检查它是否是一个TreeView节点。

If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) = False Then Exit Sub

但我试过了..

If e.Data.GetDataPresent("System.Windows.Forms.DataGridViewRow", True) = False Then Exit Sub

但这不适合我。我不确定我做错了什么?

Private Sub dataGridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles DataGridView1.MouseDown
        If DataGridView1.CurrentRow Is Nothing Then Exit Sub

        If e.Button = MouseButtons.Left And DataGridView1.CurrentCell.ColumnIndex >= 0 Then

            dragedItemText = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(0).Value.ToString
            dragedItemEXT = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(1).Value.ToString
            dragedItemRowIndex = DataGridView1.CurrentCell.RowIndex

            DataGridView1.DoDragDrop(dragedItemText, DragDropEffects.Copy)
End Sub


Public Sub TreeView1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs)
        e.Effect = DragDropEffects.Copy
        If e.Data.GetDataPresent("System.Windows.Forms.DataGridViewRow", True) Then
            Console.WriteLine("DataGridViewRow")
            e.Effect = DragDropEffects.Move
            Exit Sub
        End If
        'See if there is a TreeNode being dragged
        If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) Then
            'TreeNode found allow move effect
            e.Effect = DragDropEffects.Move
        Else
            'No TreeNode found, prevent move
            e.Effect = DragDropEffects.None
        End If

    End Sub

1 个答案:

答案 0 :(得分:1)

您正在发送文本,而不是行。

试试这个:

DataGridView1.DoDragDrop(DataGridView1.CurrentRow, DragDropEffects.Copy)