【问题标题】:Telerik CellFormatting column color is not workingTelerik CellFormatting 列颜色不起作用
【发布时间】:2012-11-10 18:13:05
【问题描述】:

我想更改 RadGrid Telerik 中列的颜色。我想给col index 2,3一个颜色,给col Index 0,1一个不同的颜色。

col 2,3 的颜色有效,但对于Col index 0,1 is not working,,索引中没有颜色 Col index 0 & 1

这是代码:

bool dontRunHandler;
private void datagridview_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 ) return;
        e.CellElement.DrawFill = true;
        e.CellElement.NumberOfColors = 1;
        e.CellElement.BackColor = Color.LightSlateGray;
        e.CellElement.GradientStyle = GradientStyles.Linear;


    }

    else
    {
        if (e.CellElement.ColumnIndex != 0 && e.CellElement.ColumnIndex != 1 ) return;
        e.CellElement.DrawFill = true;
        e.CellElement.NumberOfColors = 1;
        e.CellElement.BackColor = Color.MediumVioletRed;
        e.CellElement.GradientStyle = GradientStyles.Linear;
    }

}

【问题讨论】:

  • 我想我需要澄清一下你今天早些时候的目标。我将在中部标准时间下午 1 点左右在汤姆下午的聊天室。
  • KreepN :我在做 Telerik 的事情的地方,我不能使用聊天被禁用,包括 SO Chat,但我可以访问 SO 网络。我会告诉你,今晚我要做什么。谢谢。

标签: c# telerik telerik-grid radgrid cell-formatting


【解决方案1】:

您的代码表明它将只运行两个条件中的一个。
如果dontRunHandler =false 它将为单元格 2 和 3 着色。
否则,如果 dontRunHandler =true 它将为单元格 0 和 1 着色。
尝试删除if else,看看它是否解决了问题。

现在发生这种情况是因为第一个 if 语句返回是因为它认为您的列不是 0 或 1。

我给你的建议是改用ColunnCreated 事件。假设更快、更语义化。

  protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
    {
        if (e.Column.IsBoundToFieldName("ProductID"))
        {
            e.Column.ItemStyle.CssClass = "MyClass1";
        }
        else if (e.Column.IsBoundToFieldName("ProductName"))
        {
            e.Column.ItemStyle.CssClass = "MyClass2";
        }
    }

...
    <style type="text/css">
        .MyClass1
        {
            color: Red;
        }

         .MyClass2
        {
            color: Blue;
        }
    </style>

你认为这对你有用吗?或者您出于某种原因专门使用索引?

如果你想让你的例子起作用,我会做这样的事情:

 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 (e.CellElement.ColumnIndex == 2 || e.CellElement.ColumnIndex == 3)
        {
            e.CellElement.DrawFill = true;
            e.CellElement.NumberOfColors = 1;
            e.CellElement.BackColor = Color.LightSlateGray;
            e.CellElement.GradientStyle = GradientStyles.Linear;
        }

        else if (e.CellElement.ColumnIndex == 0 || e.CellElement.ColumnIndex == 1)
        {
            e.CellElement.DrawFill = true;
            e.CellElement.NumberOfColors = 1;
            e.CellElement.BackColor = Color.MediumVioletRed;
            e.CellElement.GradientStyle = GradientStyles.Linear;
        }

【讨论】:

  • 如果没有则删除,仍然没有变化。
  • @linguini,请看我的编辑。如果它不起作用,请告诉我
猜你喜欢
  • 2012-10-29
  • 2019-08-15
  • 2013-01-12
  • 1970-01-01
  • 1970-01-01
  • 2018-03-03
  • 1970-01-01
  • 2011-02-16
  • 1970-01-01
相关资源
最近更新 更多