列文本匹配时设置单元格BackColor

时间:2014-02-02 19:03:40

标签: c# gridview radgridview backcolor

下面的代码为radgridview的任何等于Add的单元格着色,无论列如何。当第5列中的单元格与文本值匹配时,如何将其设置为颜色?

void radGridViewFiles_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
    if (e.CellElement.Text == "Add")
    {
        e.CellElement.DrawFill = true;
        e.CellElement.BackColor = Color.Yellow;
        e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
    }
    else
    {
        e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, Telerik.WinControls.ValueResetFlags.Local);
        e.CellElement.ResetValue(VisualElement.BackColorProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
    }
}

1 个答案:

答案 0 :(得分:0)

将此代码写入代码隐藏文件:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
     LinkButton lnkbtn;
     if (e.Row.RowType == DataControlRowType.Header)
     {
          foreach (TableCell cell in e.Row.Cells)
          {
               lnkbtn = (LinkButton)cell.Controls[0];
               if (!string.IsNullOrEmpty(GridView1.SortExpression))
               {
                   if (GridView1.SortExpression.Equals(lnkbtn.Text))
                   {
                        cell.BackColor = System.Drawing.Color.Crimson;
                   }
               }
          }
     }
}