vb.net - 在 vb.net 中的 datagridview 中拖放

标签 vb.net datagridview

我在代码中使用了两个 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



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

请帮助我。

最佳答案

实际上,我只是找到了自己问题的替代方案。就是这样。

  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


我不是将 r 行中的内容复制到 Me.DataGridview. 所有单元格,而是直接将 r 行添加到名为 的数据源中Dragsaldet 。这对我来说很有效。

关于vb.net - 在 vb.net 中的 datagridview 中拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26917567/

相关文章:

c# - 从所选行开始清除 datagridview

VB.net : How can I prompt a user if they are about to close their program with unsaved data?

vb.net - 如何在VB.net中的datagridview单元格结束编辑中捕获箭头键

c# - 如何在 C# 中制作一个公共(public)秒表

.net - VB.NET动态插件组件

.net - 位置 0 处没有行

c# - 如何从 ASP.NET 页面获取当前登录的 Windows 帐户?

asp.net - 如何用今天的日期 VB.Net 填充文本框类型的日期

winforms - DataGridView右键菜单/复制示例?

javascript - 在 asp.net 和 vb.net web 应用程序中保存后如何关闭弹出窗体