Solved

Datagridview Color Individual Cells Based On Value

Posted on 2013-02-03
3
291 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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 …
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…
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 …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

896 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

12 Experts available now in Live!

Get 1:1 Help Now