Solved

Change datagrid view cell backgroud color based on color name stored in sql table

Posted on 2011-03-23
3
579 Views
Last Modified: 2012-05-11
A color for each instrument is stored in the database.  i need to change the cell background base on the instrument color assignment.
Instrument1 color.lightGray
Instrument2 color.yellow
Instrument3 color.Blue

Celle(17) has the value "color.Blue"  which of couse is a string

 Me.dgvJobStatusHistory.Rows(e.RowIndex).Cells(3).Style.BackColor = Me.dgvJobStatusHistory.Rows(e.RowIndex).Cells(17).Value

Error:  Specific cast is invalid.

Any suggestions on how to store of convert would be appreciated.
0
Comment
Question by:tipvernonia
3 Comments
 
LVL 17

Accepted Solution

by:
nepaluz earned 400 total points
ID: 35201413
pre define your colors in a dictionary and set them from there, e.g

            Dim ColorDICT As New Dictionary(Of String, Color) From {{"color.yellow", Color.Yellow}, {"color.Blue", Color.Blue}, {"color.lightGray", Color.LightGray}}
            Me.dgvJobStatusHistory.Rows(e.RowIndex).Cells(3).Style.BackColor = ColorDICT.Item(Me.dgvJobStatusHistory.Rows(e.RowIndex).Cells(17).Value)

Open in new window

0
 
LVL 16

Assisted Solution

by:Imran Javed Zia
Imran Javed Zia earned 100 total points
ID: 35201526
you can use
 If (Me.dgvJobStatusHistory.Rows(e.RowIndex).Cells(17).Value = "color.yellow") Then
            Me.dgvJobStatusHistory.Rows(e.RowIndex).Cells(3).Style.BackColor = System.Drawing.Color.Red
        ElseIf (Me.dgvJobStatusHistory.Rows(e.RowIndex).Cells(17).Value = "color.Gray") Then
            Me.dgvJobStatusHistory.Rows(e.RowIndex).Cells(3).Style.BackColor = System.Drawing.Color.Red
        ElseIf (Me.dgvJobStatusHistory.Rows(e.RowIndex).Cells(17).Value = "color.Red") Then
            Me.dgvJobStatusHistory.Rows(e.RowIndex).Cells(3).Style.BackColor = System.Drawing.Color.Red
        End If
0
 

Author Closing Comment

by:tipvernonia
ID: 35208042
I do not really want to code for every color.  Previous solution from (nepaluz) works best for me.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 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