我使用一些示例数据创建了一个datagridview。当我将datagridview中的一行拖到另一个位置时,这很好。经过数小时尝试后,我唯一无法工作的是将行拖动突出显示。 在我的代码中,行最初在行的位置突出显示。
以下截图:
如您所见,我将第8行移至第14行但行第8行而不是突出显示第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
答案 0 :(得分:0)
我为任何感兴趣的人修好了如下: 在CellMouseDown中,我设置了颜色:
Private Sub Dgv_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DgvPersonen.CellMouseDown
Dim dname As DataGridView = sender
dname.DefaultCellStyle.BackColor = Color.White
dname.DefaultCellStyle.SelectionBackColor = Color.Tomato
dname.AlternatingRowsDefaultCellStyle.BackColor = MyCustomColor
dname.AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.Tomato
当它是一个鼠标左键单击一行时,我选择该行并保存它的颜色:
dname.CurrentCell = dname(e.ColumnIndex, e.RowIndex)
OriginalColor = dname.Rows(OriginalRow).DefaultCellStyle.BackColor
dname.Rows(OriginalRow).DefaultCellStyle.BackColor = OriginalColor
dname.Rows(LastRow).DefaultCellStyle.BackColor = OriginalColor
Dim view As DataGridViewRow = DirectCast(dname.Rows(e.RowIndex), DataGridViewRow)
If view IsNot Nothing Then
dname.DoDragDrop(view, DragDropEffects.Copy)
End If
仅在拖放行时设置OriginalRow和LastRow,如下所示:
Private Sub Dgv_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DgvPersonen.DragDrop
Dim dname As DataGridView = sender
dname.DefaultCellStyle.BackColor = Color.White
dname.DefaultCellStyle.SelectionBackColor = Color.Tomato
dname.AlternatingRowsDefaultCellStyle.BackColor = MyCustomColor
dname.AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.Tomato
Dim newRow As DataGridViewRow = dname.Rows(dname.HitTest(p.X, p.Y).RowIndex)
If newRow.Index >= 0 And newRow.Index < dname.Rows.Count Then
' Get the dropped row info
Dim oldRow As DataGridViewRow = DirectCast(e.Data.GetData(GetType(DataGridViewRow)), DataGridViewRow)
' If the two rows aren't the same it inserts dropped row
If newRow.Index <> oldRow.Index Then
OriginalRow = oldRow.Index
LastRow = newRow.Index
OriginalColor = dname.Rows(OriginalRow).DefaultCellStyle.BackColor
FillDataGridViewsOnValue()
dname.Rows(OriginalRow).DefaultCellStyle.BackColor = OriginalColor
dname.Rows(LastRow).DefaultCellStyle.BackColor = Color.Tomato
dname.Rows(LastRow).Selected = True
dname.DefaultCellStyle.BackColor = Color.White
dname.DefaultCellStyle.SelectionBackColor = Color.White
dname.AlternatingRowsDefaultCellStyle.BackColor = MyCustomColor
dname.AlternatingRowsDefaultCellStyle.SelectionBackColor = MyCustomColor
这是一个很多的试验和错误,但它现在的工作原理就像是希望它能够工作。