【发布时间】:2018-07-08 08:23:04
【问题描述】:
我用一些示例数据创建了一个 datagridview。当我将 datagridview 中的一行拖到另一个位置时,效果很好。经过数小时的尝试后,我唯一无法工作的就是突出显示被拖动的行。 在我的代码中,该行在该行最初所在的位置突出显示。
下面是一些截图:
如您所见,我将第 8 行移至第 14 行,但行而不是突出显示第 14 行,它停留在第 8 行。
我的代码被剥离:
Private Sub DataGridViewForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadDataGridView1()
End Sub
Public Sub LoadDataGridView1()
' Fill datasource
End Sub
Private Sub Dgv_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DatagridView1.DragDrop
' Code to move row up or down
End Sub
Private Sub Dgv_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DatagridView1.DragEnter
e.Effect = DragDropEffects.Copy
End Sub
Private Sub DataGridView_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DatagridView1.CellMouseDown
Dim dname As DataGridView = sender
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim view As DataGridViewRow = DirectCast(dname.Rows(e.RowIndex), DataGridViewRow)
If view IsNot Nothing Then
dname.DoDragDrop(view, DragDropEffects.Copy)
End If
LoadDataGridView1()
End If
End Sub
【问题讨论】:
-
将拖放单元格的索引存储到一个变量中,然后使用该索引值设置突出显示。
-
这是绑定数据源吗?
-
LoadDataGridView1() 使用如下命令从 SQL 表中获取数据: $"SELECT * FROM {GV.DB}{MainDB} ORDER BY LISTORDER" 所以是的,它是绑定数据资源。我添加了一个额外的列 LISTODER 来操作显示行的顺序,以便您可以拖放行。但即使我插入如下命令:
-
DataGridView1.Rows(10).Selected = True 在 LoadDataGridView1() 之后它不会突出显示第 10 行,它会继续突出显示第 8 行(拖动行的位置)
-
今天我尝试了这个: LoadDataGridView1() MsgBox("After load ") DatagridView1.ClearSelection() MsgBox("After clear ") 并且在我拖放行后我收到消息“加载后" 并选择第一行,然后单击确定后没有选择任何内容,清除后出现消息,这样就可以了。在此消息上单击“确定”后,选择了最后单击的行。必须有一些内部指针来记住最后选择的行以及您所做的任何事情,即之后将选择的行。
标签: vb.net datagridview drag-and-drop selectedindex