I am attempting to setup a DataGridView on a form so that the row under the mouse is highlighted. I've got that working with the following, except the currently selected row will not highlight on MouseEnter.
The forms contains 4 separate DataGridView and the only row that is highlighted should be the one under the mouse cursor.
Private Sub dgvPrjDwgs_CellMouseEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPrjDwgs.CellMouseEnter
If e.RowIndex > -1 Then
dgvPrjDwgs.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.BlanchedAlmond
End If
End Sub
Private Sub dgvPrjDwgs_CellMouseLeave(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPrjDwgs.CellMouseLeave
If e.RowIndex > -1 Then
dgvPrjDwgs.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.DimGray
End If
End Sub
The following pic is with the mouse over a random non-selected row. The beige is the highlight color I want to show.
This pic is with the mouse over the currently selected row. I want the backcolor to change to BlanchedAlmond when the mouse is over it.
So, I changed thinking and tried using the MouseEnter to make that row the selected one. That works great. But it leaves the row selected when the mouse leaves the datagrid and moves to a different one (bad). I tried setting the selected BackColor to match the non-selected BackColor and now it doesn't highlight at all.
Private Sub dgvPrjDwgs_CellMouseEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPrjDwgs.CellMouseEnter
dgvPrjDwgs.DefaultCellStyle.SelectionBackColor = Color.BlanchedAlmond
If e.RowIndex > -1 Then
dgvPrjDwgs.Rows(e.RowIndex).Selected = True
End If
End Sub
Private Sub dgvPrjDwgs_CellMouseLeave(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPrjDwgs.CellMouseLeave
dgvPrjDwgs.DefaultCellStyle.SelectionBackColor = Color.DimGray
End Sub
Help Please :)
Got it to work.
I was using DefaultCellStyle instead of RowsDefaultCellStyle. Here is the final code.
Private Sub dgvPrjDwgs_CellMouseEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPrjDwgs.CellMouseEnter
dgvPrjDwgs.RowsDefaultCellStyle.SelectionBackColor = Color.BlanchedAlmond
If e.RowIndex > -1 Then
dgvPrjDwgs.Rows(e.RowIndex).Selected = True
End If
End Sub
Private Sub dgvPrjDwgs_CellMouseLeave(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPrjDwgs.CellMouseLeave
dgvPrjDwgs.RowsDefaultCellStyle.SelectionBackColor = Color.DimGray
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With