This is a migrated thread and some comments may be shown as answers.

ItemTemplate label backcolor

1 Answer 135 Views
Grid
This is a migrated thread and some comments may be shown as answers.
L
Top achievements
Rank 1
L asked on 20 Mar 2014, 05:14 AM
hi
I have a table field named" Colour_HEX" that has colors in HEX code in this format #808080. I tried binding it but getting an invalid cast exception error.

These are my code:

<ItemTemplate>
                <asp:Label ID="Colour_HEXLabel" runat="server" Width="100px" Height="25px" BackColor='<%# Eval("Colour_HEX") %>'></asp:Label>
</ItemTemplate>

Description: An
unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the
error and where it originated in the code.
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error: 
 
Line 107: </EditItemTemplate>
Line 108: <ItemTemplate>
Line 109: <asp:Label ID="Colour_HEXLabel" runat="server" Width="100px" Height="25px" BackColor='<%# Eval("Colour_HEX") %>'></asp:Label>
Line 110: </ItemTemplate>
Line 111: </telerik:GridTemplateColumn>

How should I solve this? Many thanks
 
 
 

           

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 Mar 2014, 08:33 AM
Hi,

You may try something like as shown below:

ASPX:
<telerik:GridTemplateColumn HeaderText="Colors" DataField="Colour_HEX">
  <ItemTemplate>
   <asp:Label ID="Colour_HEXLabel" runat="server" Text='<%# Eval("Colour_HEX") %>' ForeColor="Transparent"></asp:Label>
  </ItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
  if (e.Item is GridDataItem)
  {
    GridDataItem item = (GridDataItem)e.Item;
    Label lbl = (Label)item.FindControl("Colour_HEXLabel");
    string color = lbl.Text;
    lbl.BackColor = System.Drawing.ColorTranslator.FromHtml(color);
  }
}

Thanks,
Princy
Tags
Grid
Asked by
L
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or