[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

DataGridView - changing background of row?

Posted on 2010-08-20
15
Medium Priority
?
666 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
LVL 1

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
 
LVL 1

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
 
LVL 1

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 2000 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
 
LVL 1

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

649 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