简体   繁体   中英

Set row color of Telerik RadGrid

I am using a RadGrid for displaying the data from a database. I want to change the row color in the RadGrid to red if in the status column that row shows as "REJECTED". If the status is NULL then the row will remain displaying as the color white. I have tried this code but the row still does not change the color to red.

try
{
    if (dataBoundItem["status"].Text == "REJECTED")
    {
        TableCell cell = (TableCell)dataBoundItem["status"];
        cell.BackColor = System.Drawing.Color.Red;
        dataBoundItem.BackColor = System.Drawing.Color.Red;

        if (e.Item is GridDataItem)
        {
            GridDataItem dataBoundItem1 = e.Item as GridDataItem;

            if (dataBoundItem1["Status"].Text != null)
            {
                cell.BackColor = System.Drawing.Color.Red;
                dataBoundItem1.BackColor = Color.Red;
                dataBoundItem1["status"].ForeColor = Color.Red;
                dataBoundItem1["status"].Font.Bold = true;
            }
        }
    }
}
catch
{ }

Try something like this:

using System.Drawing;

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
 {
  if (e.Item is GridDataItem)
    {
     TableCell celltoVerify1 = dataBoundItem["status"];
     GridDataItem dataBoundItem = e.Item as GridDataItem;
       if (celltoVerify1.Text== "REJECTED")
        {
            celltoVerify1.ForeColor =  Color.Red;/// Only Change Cell Color
            dataBoundItem.ForeColor = Color.Yellow; /// Change the row Color
            //celltoVerify1.Font.Bold = true;
            //celltoVerify1.BackColor = Color.Yellow;
        }
    }
 }

Let me know if it works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM