I am trying to customize a DataGridView on a form to highlight a line under the mouse. This works for me with the following, except that the currently selected line is not highlighted by MouseEnter.
Forms contain 4 separate DataGridViews, and the only row that is highlighted should be indicated 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 figure looks with the mouse over a random unselected line. The beige color is the highlight color that I want to show.
This graphic is displayed with the mouse over the currently selected line. I want the backcolor to change to BlanchedAlmond when the mouse is over it.
So, I changed my mindset and tried to use MouseEnter to make this row selected. This works great. But it leaves the row selected when the mouse leaves the datagrid and moves to another (bad) one. I tried setting the selected BackColor to match a non-selected BackColor, and now it does not stand out 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 me please:)
source share