在 vb.net 中的 datagridview 中拖放

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26917567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:28:22  来源:igfitidea点击:

Drag and Drop in datagridview in vb.net

vb.netdatagridview

提问by Steve

I am using two Datagridviewin my code, i drag content from Me.datagridview2and drop it on Me.datagridview1.This process is successful. But as soon as i click the cell other than the dropped content cell, the dropped content disappears. Here's my code

Datagridview在我的代码中使用了两个,我从中拖动内容Me.datagridview2并将其放到Me.datagridview1.这个过程是成功的。但是,一旦我单击了除已删除的内容单元格以外的单元格,已删除的内容就会消失。这是我的代码

 Private Sub DataGridView2_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView2.MouseDown
    Try
        If Me.DataGridView2.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Me.DataGridView2.DoDragDrop(Me.DataGridView2.SelectedRows(0), DragDropEffects.All)
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

Private Sub DataGridView1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
  Try
        Dim r As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim i As Integer = Me.DataGridView1.SelectedRows(0).Index
        Me.DataGridView1.Rows(i).Cells(1).Value = r.Cells(0).Value
        Me.DataGridView1.Rows(i).Cells(2).Value = r.Cells(1).Value
        Me.DataGridView1.Rows(i).Cells(3).Value = r.Cells(2).Value
        Me.DataGridView1.Rows(i).Cells(4).Value = r.Cells(3).Value

    Catch ex As Exception
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

Private Sub DataGridView1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragEnter
    Try
        e.Effect = DragDropEffects.Copy
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub



Have following doubts
1. why does the dropped content disappears.
2. whenever we begin edit on datagridview, a row in automatically added below. Why it doesn't gets added when i drop content in datagridview.?

Please help me.



有以下疑问
1.为什么掉的内容消失了。
2.每当我们在datagridview上开始编辑时,下面会自动添加一行。为什么当我在 datagridview 中删除内容时它不会被添加。?

请帮我。

采纳答案by Steve

Actually, i just got an alternative to my own question. Here's it.

实际上,我只是有一个替代我自己的问题的方法。就是这样。

  Private Sub DataGridView1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
  Try
        Dim r As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim i As Integer = Me.DataGridView1.SelectedRows(0).Index
        dragseldet.Tables(0).Rows.Add("", r.Cells(0).Value, r.Cells(1).Value, r.Cells(2).Value, r.Cells(3).Value, 0, 0)
        dragseldet.AcceptChanges()
        'Me.DataGridView1.Rows(i).Cells(1).Value = r.Cells(0).Value
        'Me.DataGridView1.Rows(i).Cells(2).Value = r.Cells(1).Value
        'Me.DataGridView1.Rows(i).Cells(3).Value = r.Cells(2).Value
        'Me.DataGridView1.Rows(i).Cells(4).Value = r.Cells(3).Value

    Catch ex As Exception
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub


Rather than copying the contents from rrow to Me.DataGridview.all cells , i am directly adding rrow to my datasource named dragsaldet. And that did the trick for me.


我没有将行中 的内容复制rMe.DataGridview.所有单元格,而是直接将r行添加到名为dragsaldet. 这对我有用。