Programmatic Sorting
RadVirtualGrid provides an API for programmatically sorting its data. The following article will go through the events exposed for this purpose.
DataMemberNeeded
It is raised when the control is initially loaded and data related information for the given column is needed. It can be provided through the following properties that the event arguments expose.
ColumnIndex: Gives information regarding the index of the given column.
DataType: Gives information regarding the data type of the underlying member. The property can also be set.
IsFilterable: Provides information regarding whether the column generated for the given data member is filterable. The property can also be set.
IsSortable: Provides information regarding whether the column generated for the given data member is sortable. The property can also be set.
MemberName: Gives information regarding the name of the underlying data member. The property can also be set.
Example 3: Subscribing to the DataMemberNeeded event
private void VirtualGrid_DataMemberNeeded(object sender, DataMemberEventArgs e)
{
if (e.ColumnIndex == 0)
{
e.IsSortable = true;
e.DataType = typeof(string);
e.MemberName = "Name";
}
}
SortDescriptorPreparing
Raised at the state when the user clicks the header of a given column and the relevant sort descriptor is being prepared. At this state, the sorting operation can be canceled. This is done through the Cancel property of the event arguments.
Example 1: Subscribing to the SortDescriptorPreparing event
private void VirtualGrid_SortDescriptorPreparing(object sender, SortingEventArgs e)
{
if (e.ColumnIndex == 0)
{
e.Cancel = true;
}
}
SortDescriptorPrepared
It is raised when the sort descriptor is ready and the sorting operation can be executed. The event arguments provide the following properties.
ColumnIndex: The index of the column for which the sort descriptor is prepared.
ColumnSortDescriptor: Gets the generated ColumnSortDescriptor for the given column.
SortDirection: Provides information regarding the applied sorting state.
Example 2: Subscribing to the SortDescriptorPrepared event
private void VirtualGrid_SortDescriptorPrepared(object sender, SortedEventArgs e)
{
SortDescriptorCollection sortDescriptorCollection = new SortDescriptorCollection();
sortDescriptorCollection.Add(e.ColumnSortDescriptor);
clubsSource.AsQueryable().Sort(sortDescriptorCollection);
}
Note, that in the example above, the SortDescriptorCollection is used for sorting the underlying data source. For this purpose, the Sort extension method which is defined within the Telerik QueryableExtensions is utilized. More information can be found here.