MouseDown事件与CellMouseEnter事件冲突,拖拽TreeNode至DataGridView时单元格高亮失效
It sounds like the core issue here is that once you initiate a drag operation via MouseDown on the TreeNode, the mouse gets captured by the drag-and-drop system. This hijacks regular mouse events like CellMouseEnter—so the DataGridView stops receiving those hover signals while the drag is active.
Here's how to adjust your code to handle both the drag operation and cell highlighting smoothly:
Step 1: Properly Initiate the Drag from the TreeNode
First, make sure your TreeView's mouse down event starts the drag correctly:
Private Sub TreeView1_NodeMouseClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick If e.Button = MouseButtons.Left Then ' Pass the selected node as drag data TreeView1.DoDragDrop(e.Node, DragDropEffects.Copy Or DragDropEffects.Move) End If End Sub
Step 2: Use DragOver for Drag-Time Cell Highlighting
Instead of relying on CellMouseEnter (which won’t fire during drag), use the DataGridView’s DragOver event to track the hovered cell and apply selection/highlighting:
Private Sub DataGridView1_DragOver(sender As Object, e As DragEventArgs) Handles DataGridView1.DragOver ' Allow the drop operation e.Effect = DragDropEffects.Copy ' Convert drag coordinates to DataGridView local points Dim mousePos As Point = DataGridView1.PointToClient(New Point(e.X, e.Y)) Dim hitTest As DataGridView.HitTestInfo = DataGridView1.HitTest(mousePos.X, mousePos.Y) ' If we're over a valid cell, set it as current and highlight If hitTest.Type = DataGridViewHitTestType.Cell Then ' Update the current cell (matches your original CellMouseEnter behavior) DataGridView1.CurrentCell = DataGridView1(hitTest.ColumnIndex, hitTest.RowIndex) ' Optional: Add custom visual highlighting DataGridView1.Rows(hitTest.RowIndex).Cells(hitTest.ColumnIndex).Style.BackColor = Color.LightSkyBlue End If End Sub
Step 3: Clean Up Highlighting After Drag Ends
To reset the cell styling once the drag finishes or leaves the DataGridView:
Private Sub ResetCellHighlighting() ' Reset all cells to default style For Each row As DataGridViewRow In DataGridView1.Rows For Each cell As DataGridViewCell In row.Cells cell.Style.BackColor = DataGridView1.DefaultCellStyle.BackColor Next Next End Sub Private Sub DataGridView1_DragLeave(sender As Object, e As EventArgs) Handles DataGridView1.DragLeave ResetCellHighlighting() End Sub Private Sub DataGridView1_DragDrop(sender As Object, e As DragEventArgs) Handles DataGridView1.DragDrop ' Handle the drop logic (e.g., insert TreeNode data into the cell) If e.Data.GetDataPresent(GetType(TreeNode)) Then Dim droppedNode As TreeNode = DirectCast(e.Data.GetData(GetType(TreeNode)), TreeNode) Dim hitTest As DataGridView.HitTestInfo = DataGridView1.HitTest(DataGridView1.PointToClient(New Point(e.X, e.Y))) If hitTest.Type = DataGridViewHitTestType.Cell Then DataGridView1.Rows(hitTest.RowIndex).Cells(hitTest.ColumnIndex).Value = droppedNode.Text End If End If ' Reset highlighting after drop completes ResetCellHighlighting() End Sub
Key Notes
- Don’t forget to set
DataGridView1.AllowDrop = True(either in the designer or via code) to enable drop operations. - This approach uses drag-specific events that work even when the mouse is captured by the drag system, so you get both the drag functionality and real-time cell highlighting.
内容的提问来源于stack exchange,提问作者ThomasRones

