You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DataGridView拖拽行后目标行无法高亮问题求助

Fixing DataGridView Row Highlight After Drag-and-Drop

Let’s work through this highlight issue together—first, let’s break down why the original row stays lit up while the moved row doesn’t. The core problem here is twofold: you aren’t updating the grid’s selected row state after moving the row, and there’s a misplaced data reload that’s messing with row references.

Root Causes to Fix

  1. No post-drag selection update: Your current DragDrop handler moves the row but never tells the DataGridView to switch its highlight to the new position, so it sticks with the original row.
  2. Unnecessary data reload in CellMouseDown: Calling LoadDataGridView1() right before starting a drag resets your grid’s data source. This disconnects the row you’re dragging from the grid, making it impossible to track its new position later.

Step-by-Step Solutions

1. Remove the misplaced data reload

In your DataGridView_CellMouseDown method, delete the LoadDataGridView1() line. You only need to load data once when the form starts, not every time you grab a row to drag:

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
        ' Remove this line: LoadDataGridView1()
    End If
End Sub

2. Update the selection in your DragDrop handler

After moving the row to its new spot, explicitly clear the old highlight and select the newly positioned row. Here’s how to adjust your Dgv_DragDrop method (I’ve included common row-moving logic for context):

Private Sub Dgv_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DatagridView1.DragDrop
    Dim dgv As DataGridView = DirectCast(sender, DataGridView)
    If e.Data.GetDataPresent(GetType(DataGridViewRow)) Then
        ' Get the row being dragged
        Dim draggedRow As DataGridViewRow = DirectCast(e.Data.GetData(GetType(DataGridViewRow)), DataGridViewRow)
        ' Find the target drop position
        Dim targetIndex As Integer = dgv.HitTest(e.X, e.Y).RowIndex
        
        ' Make sure the target is valid and not the same as the source row
        If targetIndex >= 0 And targetIndex <> draggedRow.Index Then
            ' Clone the dragged row's data (adjust if you're moving instead of copying)
            Dim newRow As DataGridViewRow = DirectCast(draggedRow.Clone(), DataGridViewRow)
            For i As Integer = 0 To draggedRow.Cells.Count - 1
                newRow.Cells(i).Value = draggedRow.Cells(i).Value
            Next
            
            ' Remove original row and insert the cloned one at target
            dgv.Rows.Remove(draggedRow)
            dgv.Rows.Insert(targetIndex, newRow)
            
            ' Fix the highlight: clear old selection and select the new row
            dgv.ClearSelection()
            dgv.Rows(targetIndex).Selected = True
            ' Optional: scroll to the selected row so it's visible
            dgv.FirstDisplayedScrollingRowIndex = targetIndex
        End If
    End If
End Sub

Why This Works

  • Removing the extra LoadDataGridView1() call preserves the connection between the row you drag and the grid, so the drop operation can correctly track where it lands.
  • Adding ClearSelection() and Rows(targetIndex).Selected = True explicitly tells the grid which row should be highlighted after the move, overriding the stale original selection.

Give these changes a test—your dragged row should now stay highlighted in its new position instead of leaving the old row selected.

内容的提问来源于stack exchange,提问作者Miguel Terlaak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:50:19