Solved

Datagridview back color depending on cell value

Posted on 2008-10-24
4
1,085 Views
Last Modified: 2013-11-26
On the code snipet I'm changing the backcolor of the cell depending on the cell valueon a specific column, it works, but the problem is that it takes the backcolor of the last cell value found.
I mean, If the cell value "ACTIVO" is found the back color changes to green, and the if the cell value "DUPLICADO" is found the back color changes to red on every cell eventhoug it was green
So I want to mantain 2 different backcell color depending on the cell value withing the same column, is this psosible.??




Private Sub DataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
       
        For Each dr As DataGridViewRow In DataGridView1.Rows
            Select Case dr.Cells("ESTATUS").Value
                Case "ACTIVO"
                    'DataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.GreenYellow
                    'DataGridView1.RowsDefaultCellStyle.BackColor = Color.GreenYellow
                    DataGridView1.Columns("ESTATUS").DefaultCellStyle.BackColor = Color.GreenYellow
 
                Case "DUPLICADO"
                    DataGridView1.Columns("ESTATUS").DefaultCellStyle.BackColor = Color.Red
                    'DataGridView1.RowsDefaultCellStyle.BackColor = Color.Red
 
            End Select
        Next
        
    End Sub

Open in new window

0
Comment
Question by:ranhell
[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
  • 2
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
TechTiger007 earned 500 total points
ID: 22798744
this is happening because your code is modifying the default color for column of datagrid instead of the particular cell

try the code below

Private Sub DataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
        Dim DataGridView1 As New DataGridView
        For Each dr As DataGridViewRow In DataGridView1.Rows
            Select Case dr.Cells("ESTATUS").Value
                Case "ACTIVO"
                    dr.Cells("ESTATUS").Style.BackColor = Color.GreenYellow
 
                Case "DUPLICADO"
                    dr.Cells("ESTATUS").Style.BackColor = Color.Red

            End Select
        Next

    End Sub
0
 
LVL 10

Author Comment

by:ranhell
ID: 22799360
Excellent, thanks.

Now I don't mean to be abusive here but can I ask
How would I do this without using the event:
Private Sub DataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete

What I mean is if I wanted to do the same but with my on created event or Function, how would I declare it..??

I will accept your anwser as the solution is mostly a follow up question

0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22799542
The best place to do it is in RowDataBound  event handler where each row gets added to datagrid one by one.
If you want to do this in your own event handler you can declare as below

Private Sub UpdateStatus(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete

this will get called when DataBindingComplete event occurs

You can also write a seperate function to do the same task and call it from within DataGridView1_DataBindingComplete function



0
 
LVL 10

Author Closing Comment

by:ranhell
ID: 31509670
Thanks
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

627 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