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?
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?
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
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
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
Thanks zhaolai ..
ASKER
Zhaolai:
your code highlights the row nicely, but does not clear the previous row that was highlighted.
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
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.RowsDefaultC ellStyle.S electionBa ckColor = Color.Orange
Screenshot.PNG
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.RowsDefaultC
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
ASKER
jtoutou:
Cause having as row select is confusing in situations where user is entering data into individual cells
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. FullRowSel ect
.RowsDefaultCellStyle.Sele ctionBackC olor = Color.Red ' Or What ever color you want
.RowsDefaultCellStyle.Sele ctionForeC olor = Color.White
End With
This do it for you ....
Is there any different you want?Plz explain
With MyDataGridView
.SelectionMode = DataGridViewSelectionMode.
.RowsDefaultCellStyle.Sele
.RowsDefaultCellStyle.Sele
End With
This do it for you ....
Is there any different you want?Plz explain
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This hits it on its head. Thanks!
Ooaaaooo i didn't even think about that....Good job Zhaolai