Change color of specific rows (TELERIK)

user1173169 picture user1173169 · Mar 5, 2012 · Viewed 11.2k times · Source

I have in my grid a GridTemplateColumn which display "INDEF" or "MA". When it's "INDEF", i would like my row to change of color:

this is my try:

protected void grid_ItemDataBound(object sender, GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
 {
   GridDataItem item = (GridDataItem)e.Item;
   Label lbl = (Label)item.FindControl("test");
   if (lbl.Text == "INDEF")
   {
    lbl.ForeColor = System.Drawing.Color.Red;
   }
 }
}

with the code of the column in question:

              <telerik:GridTemplateColumn  HeaderText="Type de tickets"
    UniqueName="typedestickets">
    <ItemTemplate><asp:Label id="test" runat="server"></asp:Label></ItemTemplate>
</telerik:GridTemplateColumn>

But i noticed by adding a break point e.Item was not GridDataItem but GridPagerItem (i don't know why)

So i tried this: ( not working either )

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridPagerItem)
    {
        GridPagerItem item = (GridPagerItem)e.Item;
        Label lbl = (Label)item.FindControl("test");



        if (lbl.Text == "INDEF")
        {
            lbl.ForeColor = System.Drawing.Color.Red;
            item.BackColor = System.Drawing.Color.Red;
            lbl.BackColor = System.Drawing.Color.Red;

        }
    }
}

Thanks in advance for your help

Answer

PraveenVenu picture PraveenVenu · Mar 5, 2012

try this

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    //Is it a GridDataItem
    if (e.Item is GridDataItem)
    {
        //Get the instance of the right type
        GridDataItem dataBoundItem = e.Item as GridDataItem;

        //Check the formatting condition
        if (int.Parse(dataBoundItem["typedestickets"].Text) =="INDEF")
        {
            dataBoundItem["typedestickets"].ForeColor = Color.Red;
            dataBoundItem["typedestickets"].Font.Bold = true;
            //Customize more...
        }
    }
}