Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-23
3
Medium Priority
?
583 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 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