Hello Jonald,
Thank you for writing.
Due to the UI virtualization in
RadGridView, cell/row elements are created only for currently visible cells/rows and are being reused during operations like scrolling, filtering, grouping and so on.
In order to prevent applying the formatting to other columns' cell/row elements (because of the cell reuse) all customization should be reset for the rest of the cell/row elements. It is recommended to reset not only the
DrawFill property, but the all the properties you set, including
BackColor and the
GradientStyle properties. You can refer to our
Formatting Cells and
Formatting Rows help articles which are quite useful about this topic.
I am not sure why you are iterating through all the items in the list box for a specific row in the
RowFormatting event, but if a certain list box item is equal to the cell's value and you apply a specific style to the cell, the next item would not be the same and you will reset the applied settings. You should either use
break to stop the iteration when a match is found or use another approach for using the list box data inside the
RowFormatting event handler. You can find below a sample code snippet demonstrating how to change the row's back color for the currently selected item in the list box:
public
Form1()
{
InitializeComponent();
List<Item> items =
new
List<Item>();
for
(
int
i = 0; i < 30; i++)
{
items.Add(
new
Item(i,
"Item"
+ i));
}
this
.radGridView1.DataSource = items;
this
.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
this
.listBox1.DataSource = items;
this
.listBox1.DisplayMember =
"Title"
;
this
.listBox1.ValueMember =
"Id"
;
}
public
class
Item
{
public
int
Id {
get
;
set
; }
public
string
Title {
get
;
set
; }
public
Item(
int
id,
string
title)
{
this
.Id = id;
this
.Title = title;
}
}
private
void
radGridView1_RowFormatting(
object
sender, Telerik.WinControls.UI.RowFormattingEventArgs e)
{
if
((
int
)e.RowElement.RowInfo.Cells[0].Value == ((Item)listBox1.SelectedItem).Id)
{
e.RowElement.DrawFill =
true
;
e.RowElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
e.RowElement.BackColor = Color.Red;
}
else
{
e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
}
}
I hope this information helps. Should you have further questions, I would be glad to help.
Regards,
Desislava
Telerik
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time.
Watch the videos and start improving your app based on facts, not hunches.