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

x
?
Solved

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

Posted on 2011-03-23
3
Medium Priority
?
586 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 1600 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 400 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

Independent Software Vendors: 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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

916 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