Okay I researched this as much as possible before I posted so I hope this is not a repost but here goes...
I made a datatable, lets say it has a coulmn called "cartype" , then I added a column called "color"
well, I bound a gridview what I want to do is use a label, and have its FORECOLOR be the VALUE in the column "color".
I tried this:
<asp:BoundField DataField="cartype" HeaderText="Cars" ItemStyle-Width="130" ItemStyle-ForeColor='<% Eval("Color") %>' />
but I got an error about
"Cannot create an object of type System.drawing.color from its string representation '<%Eval("color")%>' for the 'ForeColor' Property.
I also tried adding a template field and got the same result.
I was trying not to use the rowdatabound event and use .Cells[3] , because then when I add columns, it's going to change the cell number, and switch up everything! I was hoping I could make it cleaner by binding the color with the data.
Okay I ended up doing this. sad :(
At the VERY END of my grid I added an ASP:Hidden template field
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hColor" runat="server" Value='<%# Eval("Color") %>'/>
</ItemTemplate>
</asp:TemplateField>
Then in the RowDataBoundEvent I did this
Dim hid As HiddenField
hid = e.Row.FindControl("hColor")
If (hid.Value <> Nothing) Then
e.Row.Cells(1).ForeColor = System.Drawing.ColorTranslator.FromHtml("#" + hid.Value)
End If
PS. I heart C#. Dont like VB.NET. lol :)
Have you tried:
ForeColor='<%# System.Drawing.Color.FromName(Eval("Color")) %>'
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With