Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Datagridview back color depending on cell value

Posted on 2008-10-24
4
Medium Priority
?
1,089 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 2000 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
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…

688 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