Solved

DataGridView - changing background of row?

Posted on 2010-08-20
15
626 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

15 Experts available now in Live!

Get 1:1 Help Now