Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Datagridview Color Individual Cells Based On Value

Posted on 2013-02-03
3
Medium Priority
?
298 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 1000 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 1000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

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 …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

927 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