Solved

Datagridview Color Individual Cells Based On Value

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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 …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 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