RadControls for WinForms

There are two events that are raised when the data in the RadGridView is sorted. The first one is the SortChanging event which is raised before the data is sorted. The second one is the SortChanged event and it is raised after the data is sorted

Copy[C#]
radGridView1.SortChanged += new Telerik.WinControls.UI.GridViewCollectionChangedEventHandler(radGridView1_SortChanged);
radGridView1.SortChanging += new Telerik.WinControls.UI.GridViewCollectionChangingEventHandler(radGridView1_SortChanging);
Copy[C#]
void radGridView1_SortChanging(object sender, Telerik.WinControls.UI.GridViewCollectionChangingEventArgs e)
{

}

void radGridView1_SortChanged(object sender, Telerik.WinControls.UI.GridViewCollectionChangedEventArgs e)
{

}
Copy[VB.NET]
Private Sub RadGridView1_SortChanging(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCollectionChangingEventArgs) Handles RadGridView1.SortChanging

End Sub

Private Sub RadGridView1_SortChanged(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCollectionChangedEventArgs) Handles RadGridView1.SortChanged

End Sub

From the event arguments of both events you can access the following data:

  • Action – an enumeration with values: Add, Remove, ItemChanged and Reset

    The Action property notifies if a SortDescriptor is added, removed, modified or the SortDescriptors collection is cleared.

  • NewItems - is a List of added, edited or removed SortDescriptors. For each SortDescriptor you can get its PropertyName and Direction

You are also able to cancel the sorting operation by setting the Cancel property to True

Copy[C#]
private void radGridView1_SortChanging1(object sender, GridViewCollectionChangingEventArgs e)
{
    e.Cancel = true;
}
Copy[VB.NET]
Private Sub RadGridView1_SortChanging1(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCollectionChangingEventArgs) Handles RadGridView1.SortChanging
    e.Cancel = True
End Sub

Since the SortDescriptors collection implements the INotifyPropertyChanged interface, you can use its CollectionChanged event:

Copy[C#]
this.radGridView1.SortDescriptors.CollectionChanged += new Telerik.WinControls.Data.NotifyCollectionChangedEventHandler(SortDescriptors_CollectionChanged);
Copy[C#]
void SortDescriptors_CollectionChanged(object sender, Telerik.WinControls.Data.NotifyCollectionChangedEventArgs e)
{

}
Copy[VB.NET]
AddHandler Me.RadGridView1.SortDescriptors.CollectionChanged, AddressOf SortDescriptors_CollectionChanged
Copy[VB.NET]
Private Sub SortDescriptors_CollectionChanged(ByVal sender As Object, ByVal e As Telerik.WinControls.Data.NotifyCollectionChangedEventArgs)

End Sub

The arguments of this event provide the same data as the SortChanged event.