AUTHOR: Dimitar Karamfilov
DATE POSTED: May 21, 2015
Problem When sorting the RadGridView logic should retrieve all object properties by using reflection. This is needed because the sorting must work with different kind of objects and should be able to compare them. Taking the property value with reflection is slow (this can be noted when you have large amount of rows in the grid) and can be avoided by using a custom comparer where the items are directly casted to the respective objects. Solution First you need to implement the custom comparer class. This class should implement the IComparer<GridViewRowInfo> interface. This interface has only one method called Compare. It takes two arguments and their type is GridViewRowInfo (In general the class should be able to compare two rows and return the result). In addition you should have access to the SortDescritors collection of the grid view. This can be achieved by passing it to group comparer constructor. This will allow you to compare the rows according to all columns sort descriptors and the current sort direction. The CompareDataItems method should be able to compare all data object properties. This way the user will be able to sort each column. [C#]
public
class
CustomComparer : IComparer<GridViewRowInfo>
{
private
SortDescriptorCollection sortDescriptors;
CustomComparer(SortDescriptorCollection sortDescriptors)
this
.sortDescriptors = sortDescriptors;
}
int
Compare(GridViewRowInfo x, GridViewRowInfo y)
GridItem item1 = (GridItem)x.DataBoundItem;
GridItem item2 = (GridItem)y.DataBoundItem;
result = 0;
for
(
i = 0; i <
.sortDescriptors.Count; i++)
result =
.CompareDataItems(item1, item2,
.sortDescriptors[i].PropertyName,
.sortDescriptors[i].Direction == ListSortDirection.Ascending);
if
(result != 0)
return
result;
CompareDataItems(GridItem x, GridItem y,
string
propertyName,
bool
ascending)
asc = (ascending) ? 1 : -1;
switch
(propertyName)
case
"TextValue"
:
x.TextValue.CompareTo(y.TextValue) * asc;
"IntValue"
x.IntValue.CompareTo(y.IntValue) * asc;
0;
Public
Class
CustomComparer
Implements
IComparer(Of GridViewRowInfo)
Private
sortDescriptors
As
SortDescriptorCollection
Sub
New
ByVal
SortDescriptorCollection)
Me
.sortDescriptors = sortDescriptors
End
Function
Compare(
x
GridViewRowInfo,
y
GridViewRowInfo)
Integer
IComparer(Of GridViewRowInfo).Compare
Dim
item1
GridItem =
DirectCast
(x.DataBoundItem, GridItem)
item2
(y.DataBoundItem, GridItem)
result
= 0
For
i
To
.sortDescriptors.Count - 1
.sortDescriptors(i).PropertyName,
.sortDescriptors(i).Direction = ListSortDirection.Ascending)
If
result <> 0
Then
Return
Next
CompareDataItems(
GridItem,
propertyName
String
,
ascending
Boolean
)
asc
=
(ascending, 1, -1)
Select
Case
x.TextValue.CompareTo(y.TextValue) * asc
x.IntValue.CompareTo(y.IntValue) * asc
0
radGridView1.MasterTemplate.SortComparer =
new
CustomComparer(
.radGridView1.SortDescriptors);
.radGridView1.SortDescriptors)
partial
RadForm1 : Telerik.WinControls.UI.RadForm
Random rnd =
Random();
BindingList<GridItem> data;
RadForm1()
InitializeComponent();
data =
BindingList<GridItem>();
i = 0; i < 100000; i++)
data.Add(
GridItem(
"Text "
+ rnd.Next(100), i));
radGridView1.DataSource = data;
GridItem
TextValue {
get
;
set
; }
IntValue {
textValue,
intValue)
.TextValue = textValue;
.IntValue = intValue;
Partial
RadForm1
Inherits
Telerik.WinControls.UI.RadForm
rnd
Random()
data
BindingList(Of GridItem)
()
InitializeComponent()
BindingList(Of GridItem)()
99999
& rnd.
(100), i))
radGridView1.DataSource = data
Property
TextValue()
IntValue()
textValue
intValue
.TextValue = textValue
.IntValue = intValue
Resources Buy Try