I have a checkboxcolumn in my radgridview, and when the user set to true a cell, the backcolor of the current row changes . That's working fine. But when I scroll in my View, the backcolor of the row is moved to other row. The second row gets the backcolor of the firstrow and so on.
The code is :
private void radGridView1_ValueChanged(object sender, EventArgs e) |
{ |
if (this.radGridView1.ActiveEditor is RadCheckBoxEditor) |
{ |
if ((bool)(this.radGridView1.ActiveEditor.Value)) |
{ |
radGridView1.CurrentCell.RowElement.DrawFill = true; |
radGridView1.CurrentCell.RowElement.BackColor = Color.Red; |
} |
} |
} |
What can the resolve this?
10 Answers, 1 is accepted
The ValueChanged event is not appropriate to change the appearance of visual elements. The described behavior is caused by grid virtualization feature. RadGridView reuses visual elements (rows and cells) to increase its performance and decrease its memory footprint. You should use RowFormatting instead. You can use the following code to do that:
private
void
radGridView_RowFormatting(
object
sender, RowFormattingEventArgs e)
{
GridViewDataRowInfo dataRow = e.RowElement.RowInfo
as
GridViewDataRowInfo;
if
(dataRow ==
null
)
{
return
;
}
object
columnValue = dataRow.Cells[
"YourBoolColumn"
].Value;
if
(columnValue !=
null
&& Convert.ToBoolean(columnValue))
{
e.RowElement.DrawFill =
true
;
e.RowElement.BackColor = Color.Red;
}
else
{
e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
}
}
Sincerely yours,
Svett
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
I've other problem: I need change the row's forecolor. Then, I change your code for:
private void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e) |
{ |
GridViewDataRowInfo dataRow = e.RowElement.RowInfo as GridViewDataRowInfo; |
if (dataRow == null) |
{ |
return; |
} |
object columnValue = dataRow.Cells["column1"].Value; |
if (columnValue != DBNull.Value && columnValue!= null && Convert.ToBoolean(columnValue)) |
{ |
e.RowElement.DrawFill = true; |
e.RowElement.ForeColor = Color.Red; |
} |
else |
{ |
e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local); |
e.RowElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local); |
} |
} |
The problem is: The text in the row continue being black color. ¿why?
Thank you,
Help me please, I need solution this problem. I have to change the row's forecolor, but e.rowelement.forecolor don't change the text color.
If you need to change the fore color, you have to change cell's fore color instead of row fore color. You can do that in CellFormatting event. You can use the following code snippet as sample:
private
void
radGridView_CellFormatting(
object
sender, CellFormattingEventArgs e)
{
GridViewDataRowInfo dataRow = e.CellElement.RowInfo
as
GridViewDataRowInfo;
if
(dataRow ==
null
)
{
return
;
}
object
columnValue = dataRow.Cells[
"column1"
].Value;
if
(columnValue != DBNull.Value && columnValue !=
null
&& Convert.ToBoolean(columnValue))
{
e.CellElement.ForeColor = Color.Red;
}
else
{
e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
}
}
A bit off topic, please ask the person who has purchased our controls in your company to add you as a License Developer to the purchase. This will give you full access to the products your company has purchased, to our downloads section, and to our support ticketing system. Additionally, all your questions will be reviewed according to the license you have. More information on License Developers you can find here: www.telerik.com/account/faqs.aspx.
Sincerely yours,
Svett
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Thank you, but I have other problem with your code. I need each row, whose cell is check to true, have a different color. However, when I set to true the second, third,... row, all the rows, whose cell is check to true , are setting to the same color, exactly to the last select color.
How do I this property?
Thank you,
Fernan
You can use the Tag property of GridViewRowInfo to store the desired color. Then you can get and apply it to the visual row element. You can use the following code snippet:
private
void
radGridView_RowFormatting(
object
sender, RowFormattingEventArgs e)
{
GridViewDataRowInfo dataRow = e.RowElement.RowInfo
as
GridViewDataRowInfo;
if
(dataRow ==
null
)
{
return
;
}
object
columnValue = dataRow.Cells[
"column1"
].Value;
if
(columnValue != DBNull.Value && columnValue !=
null
&& Convert.ToBoolean(columnValue))
{
if
(e.RowElement.RowInfo.Tag ==
null
)
{
e.RowElement.RowInfo.Tag =
this
.GetNextBackGroundColor();
}
e.RowElement.DrawFill =
true
;
e.RowElement.BackColor = (Color)e.RowElement.RowInfo.Tag;
}
else
{
e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
e.RowElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
}
}
private
Color GetNextBackGroundColor()
{
// This is factory method that produces the next new background color
}
You only need to implement your algorithm for determining the different background colors. You have to place it inside GetNextBackGroundColor method.
All the best,
Svett
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Thanks lot
I am having the same problem. Using rowFormatting does not solve this for me. The row color sticks when scrolling.
private void gvFumigation_RowFormatting(object sender, RowFormattingEventArgs e)
{
GridViewDataRowInfo dataRow = e.RowElement.RowInfo as GridViewDataRowInfo;
if (dataRow == null)
{
return;
}
if (dataRow.Cells["CompleteDate"].Value == null)
return;
if (dataRow.Cells["CutDate"].Value == null)
return;
//Get cut and release date
DateTime releaseDate = new DateTime();
DateTime cutDate = new DateTime();
releaseDate = (DateTime)dataRow.Cells["CompleteDate"].Value;
cutDate = (DateTime)dataRow.Cells["CutDate"].Value;
//Get release date at 3pm
TimeSpan span3PM = new TimeSpan(15,0,0);
DateTime release3PM = new DateTime();
release3PM = releaseDate.Date.Add(span3PM);
//If release date is after cut date, highlight row red.
if (releaseDate > cutDate)
{
e.RowElement.DrawFill = true;
e.RowElement.GradientStyle = GradientStyles.Solid;
e.RowElement.BackColor = Color.Red;
}
//If release date is the day before cut date and after 3pm, highlight row yellow
else if (releaseDate.Date == cutDate.Date.AddDays(-1))
{
if (releaseDate >= release3PM)
{
e.RowElement.DrawFill = true;
e.RowElement.GradientStyle = GradientStyles.Solid;
e.RowElement.BackColor = Color.Yellow;
}
}
else
{
//leave everything as is
e.RowElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local);
e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, Telerik.WinControls.ValueResetFlags.Local);
}
}
I am glad that the problem you were facing is now resolved. However, in case someone from the community faces a similar problem it would be good to have a look at the following help articles for further information:
https://docs.telerik.com/devtools/winforms/gridview/cells/formatting-cells
https://docs.telerik.com/devtools/winforms/gridview/rows/formatting-rows
I hope this information helps.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik