Solved

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

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now