Solved

Datagridview back color depending on cell value

Posted on 2008-10-24
4
1,083 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

737 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