3

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

1 Answers1

5

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...
        }
    }
}
PraveenVenu
  • 8,217
  • 4
  • 30
  • 39
  • thanks for your fast reply but i don't understand you code , what is "TicketType supposed to be ? Why do you try to cast databounditem to Int? thanks –  Mar 05 '12 at 10:45
  • I meant TicketType by the column name and int as the type of the column. Ex: if you are binding the column with name "Column1" of type string to the label, then use dataBoundItem["Column1"].Text – PraveenVenu Mar 05 '12 at 10:47
  • The uniqueName of my GridTemplateColumn is typedestickets. So i tried with : if (int.Parse(dataBoundItem["typedestickets"].Text) > 100) { dataBoundItem["typedestickets"].ForeColor = Color.Red; dataBoundItem["typedestickets"].Font.Bold = true; //Customize more... } But it's not working Error "Input String was not in correct format" –  Mar 05 '12 at 10:57