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

Changing ForeColor based on value

1 Answer 343 Views
Grid
This is a migrated thread and some comments may be shown as answers.
L
Top achievements
Rank 1
L asked on 27 Mar 2014, 04:55 AM
hi

I am receiving  System.InvalidCastException: Specified cast is not valid.

I want to change the font color based on value of a label in the grid

this is my code:

<telerik:GridTemplateColumn DataField="Age" DataType="System.int"
            FilterControlAltText="Filter Age column" HeaderText="Age"
            SortExpression="Age" UniqueName="Age">
            <EditItemTemplate>
                <asp:TextBox ID="AgeTextBox"  Width="35px" runat="server"
                    Text='<%# Bind("Age") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="AgeLabel" runat="server" ForeColor='<%# IIf(DataBinder.Eval(Container.DataItem, "Age") = "0", "White", "Black")%>'
                    Text='<%# Eval("Age") %>'></asp:Label>
            </ItemTemplate>

How should i solve this? Many thanks in advance.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 27 Mar 2014, 05:21 AM
Hi,

You can set the fore color from the code behind in the ItemDataBound event of the RadGrid as follows:

VB:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim lbl As Label = DirectCast(item.FindControl("AgeLabel"), Label)
        If lbl.Text = "0" Then
            lbl.ForeColor = Color.White
        End If
    End If
End Sub

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