Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change backcolor of selected row in datagridview on mouseover

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.

Highlight, highlight, you so fine, you so fine you blow my mind!

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.

Why you no highlight when I want.

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 :)

like image 541
bluesixty Avatar asked Apr 03 '12 00:04

bluesixty


1 Answers

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
like image 147
bluesixty Avatar answered Sep 26 '22 02:09

bluesixty