Solved

Datagridview back color depending on cell value

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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 …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

810 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