Solved

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

Posted on 2011-03-23
3
578 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

789 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