Solved

DataGridView - changing background of row?

Posted on 2010-08-20
15
634 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now