更改特定行的颜色(TELERIK)

时间:2012-03-05 10:35:25

标签: c# asp.net telerik radgrid

我的网格中有一个显示“INDEF”或“MA”的GridTemplateColumn。当它是“INDEF”时,我希望我的行改变颜色:

这是我的尝试:

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;
   }
 }
}

使用相关列的代码:

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

但我注意到添加了一个断点e.Item不是GridDataItem而是GridPagerItem(我不知道为什么)

所以我试过这个:(也没有工作)

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;

        }
    }
}

提前感谢您的帮助

1 个答案:

答案 0 :(得分:5)

试试这个

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...
        }
    }
}