• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 671
  • Last Modified:

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?
0
Jess31
Asked:
Jess31
  • 7
  • 4
  • 4
1 Solution
 
ZhaolaiCommented:
The easiest way is to set the datagridview's SelectionMode property to FullRowSelect.

0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
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

0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
the second line of my code is not necessary
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Thanks zhaolai ..
0
 
Jess31Author Commented:
Zhaolai:
your code highlights the row nicely, but does not clear the previous row that was highlighted.
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Why don't you use our first option ?fullrowselect
and if you want change the selection backcolor
0
 
ZhaolaiCommented:
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
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
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

0
 
Jess31Author Commented:
jtoutou:
Cause having as row select is confusing in situations where user is entering data into individual cells
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
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
0
 
Jess31Author Commented:
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
0
 
ZhaolaiCommented:
Try this (It works for me):
At design time, set the SelectionMode to FullRowSelect. Then add the following code:

    Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
        DataGridView1.BeginEdit(True)
    End Sub


0
 
Jess31Author Commented:
This hits it on its head. Thanks!
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Ooaaaooo i didn't even think about that....Good job Zhaolai
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 7
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now