Link to home
Start Free TrialLog in
Avatar of Jess31
Jess31

asked on

DataGridView - changing background of row?

I'm using vb.net and datagridview.
When I click a cell how can I have the background color of that entire row change, and when clicking in another row then the first one would go back to normal and the new one will have its background color change?
Avatar of Zhaolai
Zhaolai
Flag of United States of America image

The easiest way is to set the datagridview's SelectionMode property to FullRowSelect.

1.You have an option to make the selection Mode to Full Row Select
2.you can handle cell Click and add this code to change row color

Private Sub MYDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles MYDataGridView.CellClick
     If Not IsDBNull(Me.MyDataGridView.Rows(e.RowIndex.Cells(YOurCell).Value) Or Me.MyDataGridView.Rows(e.RowIndex.Cells(YOurCell).Value Then   
           Me.MyDataGridView.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.AliceBlue
     End If
    End Sub

Open in new window

the second line of my code is not necessary
To supplement @jtoutou's second method, you need to clear the select before setting the row color. Code here:

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        DataGridView1.ClearSelection()
        DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.AliceBlue
    End Sub

Open in new window

Avatar of Jess31
Jess31

ASKER

Zhaolai:
your code highlights the row nicely, but does not clear the previous row that was highlighted.
Why don't you use our first option ?fullrowselect
and if you want change the selection backcolor
The FullRowSelect should work for you.
If you want to set to a different color other than the default, set it in the RowsDefaultCellStyle property, either in design time or in run time.

To set it in design time, see the attached screenshot.
To set it in run time, here is the code:
        DataGridView1.RowsDefaultCellStyle.SelectionBackColor = Color.Orange



Screenshot.PNG
Put this in your form load event

With MyDataGridView
            .RowsDefaultCellStyle.SelectionBackColor = Color.Red ' Or What ever color you want
            .RowsDefaultCellStyle.SelectionForeColor = Color.White
End With

Open in new window

Avatar of Jess31

ASKER

jtoutou:
Cause having as row select is confusing in situations where user is entering data into individual cells
But this was your initial request..."background color of that entire row change"
With MyDataGridView
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .RowsDefaultCellStyle.SelectionBackColor = Color.Red ' Or What ever color you want
            .RowsDefaultCellStyle.SelectionForeColor = Color.White
End With
This do it for you ....
Is there any different you want?Plz explain
Avatar of Jess31

ASKER

jtoutou:
yes, I want to highlight the entire row. But using the FullRowSelect I lose the ability to discern which cell i'm on.So I need to be able to change the background color of the current row w/o change the selection mode to FullRowSelect
ASKER CERTIFIED SOLUTION
Avatar of Zhaolai
Zhaolai
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jess31

ASKER

This hits it on its head. Thanks!
Ooaaaooo i didn't even think about that....Good job Zhaolai