Solved

DataGridView - changing background of row?

Posted on 2010-08-20
15
651 Views
Last Modified: 2013-11-08
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
Comment
Question by:Jess31
  • 7
  • 4
  • 4
15 Comments
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33486210
The easiest way is to set the datagridview's SelectionMode property to FullRowSelect.

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33486237
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33486327
the second line of my code is not necessary
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 17

Expert Comment

by:Zhaolai
ID: 33486429
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33486629
Thanks zhaolai ..
0
 

Author Comment

by:Jess31
ID: 33486850
Zhaolai:
your code highlights the row nicely, but does not clear the previous row that was highlighted.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33486888
Why don't you use our first option ?fullrowselect
and if you want change the selection backcolor
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33487283
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33487398
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
 

Author Comment

by:Jess31
ID: 33487539
jtoutou:
Cause having as row select is confusing in situations where user is entering data into individual cells
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33487597
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
 

Author Comment

by:Jess31
ID: 33488279
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
 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
ID: 33488548
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
 

Author Closing Comment

by:Jess31
ID: 33488593
This hits it on its head. Thanks!
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33488808
Ooaaaooo i didn't even think about that....Good job Zhaolai
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question