Reputation: 1939
I want to change color for the columns in the RadGrid Telerik. I want to give a color to the col index 2,3
and different color the to col Index 0,1
.
The color for col 2,3 is working but forCol index 0,1 is not working,
there is no color in the index Col index 0 & 1
This is the code:
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;
}
}
Upvotes: 1
Views: 1158
Reputation: 3385
Your code shows that it will run only one condition out of two.
If dontRunHandler =false
it will color cells 2 and 3.
else if dontRunHandler =true
it will color cells 0 and 1.
Try to remove if else, and see if it solves a problem.
now this happens because first if statement returns because it thinks that your column is not 0 or 1.
My suggestion to you is to use ColunnCreated event instead. Suppose to be faster and more semantic.
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>
Do you think this is something that will work for you. Or you are specifically using indexes for some reason?
if you want your example to work, I would do something like this:
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;
}
Upvotes: 1