2012-11-10 64 views
1

我想更改RadGrid Telerik中列的颜色。我想给颜色col index 2,3和不同颜色的col Index 0,1Telerik CellFormatting列颜色不起作用

的颜色山坳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; 
    } 

} 

enter image description here

+0

我想我需要澄清你是什么今天早些时候之后。我将于下午1点左右在CST的聊天室汤姆下午。 – KreepN

+0

KreepN:我正在做这个Telerik的东西的地方,我不能使用聊天功能被禁用,包括SO聊天,但我可以访问SO网络。我会让你知道,今天晚上我该做什么。谢谢。如果其他人删除 – linguini

回答

1

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

现在出现这种情况,因为第一个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; 
     } 
+0

,仍然没有改变。 – linguini

+0

@linguini,请参阅我的编辑。让我知道如果它不起作用 – RAS

+0

谢谢,它的工作原理。 – linguini