Solved

Datagridview Color Individual Cells Based On Value

Posted on 2013-02-03
3
292 Views
Last Modified: 2013-02-05
I have a datagridview that gets data from SQL.  In 14 of the 17 columns there will be a value of either a 1 or a 0.  I want to loop through just those columns and color all 0 values green and all 1 values red.  I don't want to color the whole row, just these columns.

I don't have any sample code to show as I have no idea where to start. I have nosed around here on EE, but haven't found anything.  Thanks for any assistance you can provide.
0
Comment
Question by:G Scott
3 Comments
 
LVL 6

Accepted Solution

by:
esolve earned 250 total points
ID: 38848699
You do not have to iterate over every row. Just do it in the RowDatabound event. This event will fire after each row has been bound to the data.

This is your code to retrieve the data:
   Dim ds As New DataSet()
        Dim cn As New SqlConnection("SERVER=.;DATABASE=School;UID=sa;PWD=;")
        Dim cm As New SqlCommand("SELECT IsEnabled, * FROM Table", cn)
        Dim da = New SqlDataAdapter(cm)

        da.Fill(ds)

        GridView1.DataSource = ds
        GridView1.DataMember = ds.Tables(0).TableName
        GridView1.DataBind()

Open in new window


This is your event to change the colors:
 Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim bEnabled As Boolean
            bEnabled = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "IsEnabled"))

            If bEnabled = True Then
                e.Row.Cells(0).BackColor = System.Drawing.Color.Red
            Else
                e.Row.Cells(0).BackColor = System.Drawing.Color.Green
            End If
        End If

    End Sub

Open in new window

0
 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 250 total points
ID: 38848829
Another way
Try to use the Cell formating Event ....
 Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        If e.ColumnIndex = Me.DataGridView1.Columns(1).Index Then
            If e.Value = 0 Then
                e.CellStyle.BackColor = Color.green
            ElseIf e.Value = 1 Then
                e.CellStyle.BackColor = Color.Red
            End If
        End If
    End Sub

Open in new window

Note :this is just for one Column sample
0
 
LVL 1

Author Closing Comment

by:G Scott
ID: 38855345
Both of these worked perfectly.  Thanks to you both.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now