Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to set datagridView row back color from datatable value.
VB
Dim strColor As String = DataGrigView1.CurrentRow.Cells("CalColor").Value.ToString()
grdJobTracking.CurrentRow.DefaultCellStyle.SelectionBackColor = System.Drawing.Color.FromArgb(strColor)

But here it throw an error message

Conversion from string "Red " to type 'Integer' is not valid.

how I can set the back color of grid.

Thanks!

Kuldeep Dwivedi
Posted
Updated 10-Nov-11 5:54am
v2
Comments
Sergey Alexandrovich Kryukov 10-Nov-11 11:55am    
Wrong question! This is complete lack of elementary logic: if a compiler tells you: "expected type is A, and your supply B" ("conversion from 'B' to 'A' is not valid") and you are asking about converting from B to A. Isn't that simple?

You say, "conversion is invalid; so help me do this conversion". Wow!
--SA

1 solution

Well, there is a factory static method System.Drawing.Color.FromName, please see, but it can only work if the string parameter is equal to the name of one of predefined colors. The colors are enumerated in the type System.Drawing.KnownColor. This will construct an instance of System.Drawing.Color structure.

See:
http://msdn.microsoft.com/en-us/library/system.drawing.color.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.knowncolor.aspx[^].

However, this is not you may really need. Please see my comment to the question.

Even though it works, this is unreliable: how can you guarantee valid color names all the time? What if software definitions change with time and your database data will survive? Is it all about storing the color in data base? It could be much better to store separate A, R, G, B or just R, G, B components of the color, or a color in one hexadecimal string which you would parse into A, R, G, B. This is how colors are represented in CSS (see); so you can use that format or something like that — see http://www.w3schools.com/cssref/css_colors.asp[^].

—SA
 
Share this answer
 
v2
Comments
thatraja 10-Nov-11 12:13pm    
You beat me to it man, 5!
Sergey Alexandrovich Kryukov 10-Nov-11 12:23pm    
Ha-ha. :-) Thank you, Raja.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900