Solved

Datagridview Color Individual Cells Based On Value

Posted on 2013-02-03
3
290 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
Comment Utility
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
Comment Utility
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
Comment Utility
Both of these worked perfectly.  Thanks to you both.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

15 Experts available now in Live!

Get 1:1 Help Now