如何通过cellFormatting Telerik覆盖单元格颜色

时间:2012-11-08 21:19:41

标签: c# .net winforms telerik radgridview

我在Telerik上,&我正在尝试格式化col值。列索引号12已经被赋予颜色(它可以工作),然后我想查找特定值,如果它存在则更改单元格颜色。如果单元格值为4354,则更改单元格颜色....但没有发生任何事情。 以下代码中的错误是什么?

private int cellValue = 4354;

for (int i = 0; i < TelerikRadGridView.Rows.Count; i++)
 {
 if (TelerikRadGridView.Rows[i].Cells[i].Value == (object)cellValue)
      {
            e.CellElement.BackColor = Color.Gold;
         }
   }

这是完整的代码:

private int cellValue = 4354;
private void TelerikRadGridView_CellFormatting(object sender, CellFormattingEventArgs e)
    {

        e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);

        if (dontRunHandler == false)
            {
                if (e.CellElement.ColumnIndex != 2 
                && e.CellElement.ColumnIndex != 3 
                && e.CellElement.ColumnIndex != 4
                && e.CellElement.ColumnIndex != 5 
                && e.CellElement.ColumnIndex != 7 
                && e.CellElement.ColumnIndex != 11 
                && e.CellElement.ColumnIndex != 12 
                && e.CellElement.ColumnIndex != 13) return;
                e.CellElement.DrawFill = true;
                e.CellElement.NumberOfColors = 1;
                e.CellElement.BackColor = Color.LightSlateGray;
                e.CellElement.GradientStyle = GradientStyles.Linear;
            }
            for (int i = 0; i < TelerikRadGridView.Rows.Count; i++)
            {
                if (TelerikRadGridView.Rows[i].Cells[i].Value == (object)cellValue)
                {
                    e.CellElement.BackColor = Color.Gold;
                }
            }

1 个答案:

答案 0 :(得分:2)

为你重新编写它,这样你就可以做到:

private int cellValue = 4354;
private void TelerikRadGridView_CellFormatting(object sender, CellFormattingEventArgs e)
{
    e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
    e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
    e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
    e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);

    if (dontRunHandler == false)
    {
        if (e.CellElement.ColumnIndex != 2 
        && e.CellElement.ColumnIndex != 3 
        && e.CellElement.ColumnIndex != 4
        && e.CellElement.ColumnIndex != 5 
        && e.CellElement.ColumnIndex != 7 
        && e.CellElement.ColumnIndex != 11 
        && e.CellElement.ColumnIndex != 12 
        && e.CellElement.ColumnIndex != 13) return;
        else
        {
           if(e.CellElement.Value != null)
           {
               if (Double.Parse(e.CellElement.Value.ToString()) != cellValue)
               {
                   e.CellElement.DrawFill = true;
                   e.CellElement.NumberOfColors = 1;
                   e.CellElement.BackColor = Color.LightSlateGray;
                   e.CellElement.GradientStyle = GradientStyles.Linear;
               }
               else
               {
                   e.CellElement.DrawFill = true;
                   e.CellElement.NumberOfColors = 1;
                   e.CellElement.BackColor = Color.Gold;
                   e.CellElement.GradientStyle = GradientStyles.Linear;
               }
           }
        }
    }