This topic uses code example to demonstrate how to perform external sorts using xamDataGrid™ control.
The following topics are prerequisites to understanding this topic:
This topic contains the following sections:
Using the xamDataGrid’s external sorting of bound fields, whenever a user sorts on a field, is quicker than internal sorting. The CollectionView works by taking over the requested operation (sorting), and after the operation is complete, the CollectionView
propagates the changes to the data presenter and the data presenter reflects the changes by re-displaying the data in the specified order.
In order to use the external sorting you need to set the FieldSettings
's SortEvaluationMode property to either Manual or UseCollectionView, depending on your data source type. The available options of the enumerations are:
The following screenshot illustrates the xamDataGrid rendering before sort operation on the Price column outlining that the selected option in SortEvaluationMode
is UseCollectionView
.
The next screenshot illustrates after external sort operation of the outlined column (Price) in descending order.
This example demonstrates how to configure the external sort process in xamDataGrid . With this process the data presenter performs the sort operation based on ICollectionView
's SortDescription
, rather than relying on the data presenter to perform the sort process internally using its own collection of routines.
The following code examples will show the sort operation being setup to perform externally. There is less overhead with external sort process, because the data presenter does not use its resources for sorting the records. Instead the CollectionView
performs the process and after completing the process the CollectionView
notifies the data presenter so the data presenter is ready to display the final result in the UI.
Create a new WPF Application project and perform the following requirements:
Add the following NuGet package to your application:
Infragistics.WPF.DataGrids
Add the following namespace definitions in the XAML part of the window, where your will place the mark-up for the xamDataGrid:
xmlns:igDP=http://infragistics.com/DataPresenter
Ensure that the relevant properties of the object are marked as public , as those properties will be accessed using Reflection while evaluating filter conditions.
Setting the SortEvaluationMode
with UseCollectionView option.
In XAML:
<igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayoutSettings SortEvaluationMode="UseCollectionView" />
</igDP:XamDataGrid.FieldLayoutSettings>
Setting up a resource that represents the SortEvaluationMode
object.
In C#:
This.Resources.Add("SortEvaluationMode", Enum.GetValues(typeof(SortEvaluationMode)));
In Visual Basic:
This.Resources.Add(SortEvaluationMode, [Enum].GetValues(GetType(SortEvaluationMode)))
Setting an external sort for a desired field.
In C#:
ICollectionView iCollectionView = (ICollectionView)this.Resources["sort_DataSource"];
iCollectionView.SortDescriptions.Clear();
iCollectionView.SortDescriptions.Add(new SortDescription("Price", ListSortDirection.Descending));
In Visual Basic:
Dim iCollectionView As ICollectionView = DirectCast(Me.Resources(sort_DataSource), ICollectionView)
iCollectionView.SortDescriptions.Clear()
iCollectionView.SortDescriptions.Add(New SortDescription(Price, ListSortDirection.Descending))
Data model definition.
In C#:
public class DataModel : INotifyPropertyChanged
{
private string _make;
public string Make
{
get { return _make; }
set
{
if (_make != value)
{
_make = value;
OnPropertyChanged("Make");
}
}
}
private string _model;
public string Model
{
get { return _model; }
set
{
if (_model != value)
{
_model = value;
OnPropertyChanged("Model");
}
}
}
private double _price;
public double Price
{
get { return _price; }
set
{
if (_price != value)
{
_price = value;
OnPropertyChanged("Price");
}
}
}
private int _mileage;
public int Mileage
{
get { return _mileage; }
set
{
if (_mileage != value)
{
_mileage = value;
OnPropertyChanged("Mileage");
}
}
}
#region INotifyPropertyChanged Members
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
In Visual Basic:
Public Class DataModel
Implements INotifyPropertyChanged
Private _make As String
Public Property Make() As String
Get
Return _make
End Get
Set
If _make <> value Then
_make = value
OnPropertyChanged("Make")
End If
End Set
End Property
Private _model As String
Public Property Model() As String
Get
Return _model
End Get
Set
If _model <> value Then
_model = value
OnPropertyChanged("Model")
End If
End Set
End Property
Private _price As Double
Public Property Price() As Double
Get
Return _price
End Get
Set
If _price <> value Then
_price = value
OnPropertyChanged("Price")
End If
End Set
End Property
Private _mileage As Integer
Public Property Mileage() As Integer
Get
Return _mileage
End Get
Set
If _mileage <> value Then
_mileage = value
OnPropertyChanged("Mileage")
End If
End Set
End Property
#Region "INotifyPropertyChanged Members"
Protected Sub OnPropertyChanged(propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler
#End Region
End Class
In XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button x:Name="sortBtn" Content="Sort" Width="100"
HorizontalAlignment="Stretch"
Click="OnSort_click"/>
<Button x:Name="filterBtn" Content="Filter" Width="100"
HorizontalAlignment="Stretch"
Click="OnFilter_click"/>
</StackPanel>
<igDP:XamDataGrid x:Name="_sortGrid"
Grid.Row="1"
DataSource="{DynamicResource sort_DataSource}"
VerticalAlignment="Stretch">
<igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayoutSettings SortEvaluationMode="UseCollectionView" />
</igDP:XamDataGrid.FieldLayoutSettings>
</igDP:XamDataGrid>
</Grid>
In C#:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Resources.Add("SortEvaluationMode", Enum.GetValues(typeof(SortEvaluationMode)));
this.CreateSortDataSource();
}
private void OnSort_click(object sender, RoutedEventArgs e)
{
ICollectionView iCollectionView = (ICollectionView)this.Resources["sort_DataSource"];
iCollectionView.SortDescriptions.Clear();
iCollectionView.SortDescriptions.Add(new SortDescription("Price", ListSortDirection.Descending));
}
private void CreateSortDataSource()
{
// Populate some data
var data = new ObservableCollection<DataModel>();
data.Add(new DataModel() { Make = "Ford", Model = "Mustang", Price = 25000, Mileage = 1000 });
data.Add(new DataModel() { Make = "Jeep", Model = "Wrangler", Price = 21000, Mileage = 200 });
data.Add(new DataModel() { Make = "Honda", Model = "Accord", Price = 19500, Mileage = 1000 });
data.Add(new DataModel() { Make = "Toyota", Model = "Camry", Price = 22500, Mileage = 500 });
data.Add(new DataModel() { Make = "Ford", Model = "Escort", Price = 15000, Mileage = 15000 });
data.Add(new DataModel() { Make = "Toyota", Model = "4Runner", Price = 33000, Mileage = 50 });
data.Add(new DataModel() { Make = "Honda", Model = "Pilot", Price = 35000, Mileage = 18000 });
data.Add(new DataModel() { Make = "Jeep", Model = "Patriot", Price = 22000, Mileage = 1000 });
data.Add(new DataModel() { Make = "Honda", Model = "Civic", Price = 19500, Mileage = 30000 });
// Add the ListCollectionView as a resource
this.Resources.Add("sort_DataSource", new ListCollectionView(data));
}
}
In Visual Basic:
Public Partial Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
Me.Resources.Add("SortEvaluationMode", [Enum].GetValues(GetType(SortEvaluationMode)))
Me.CreateSortDataSource()
End Sub
Private Sub OnSort_click(sender As Object, e As RoutedEventArgs)
Dim iCollectionView As ICollectionView = DirectCast(Me.Resources("sort_DataSource"), ICollectionView)
iCollectionView.SortDescriptions.Clear()
iCollectionView.SortDescriptions.Add(New SortDescription("Price", ListSortDirection.Descending))
End Sub
Private Sub CreateSortDataSource()
' Populate some data
Dim data = New ObservableCollection(Of DataModel)()
data.Add(New DataModel() With {
.Make = "Ford",
.Model = "Mustang",
.Price = 25000,
.Mileage = 1000
})
data.Add(New DataModel() With {
.Make = "Jeep",
.Model = "Wrangler",
.Price = 21000,
.Mileage = 200
})
data.Add(New DataModel() With {
.Make = "Honda",
.Model = "Accord",
.Price = 19500,
.Mileage = 1000
})
data.Add(New DataModel() With {
.Make = "Toyota",
.Model = "Camry",
.Price = 22500,
.Mileage = 500
})
data.Add(New DataModel() With {
.Make = "Ford",
.Model = "Escort",
.Price = 15000,
.Mileage = 15000
})
data.Add(New DataModel() With {
.Make = "Toyota",
.Model = "4Runner",
.Price = 33000,
.Mileage = 50
})
data.Add(New DataModel() With {
.Make = "Honda",
.Model = "Pilot",
.Price = 35000,
.Mileage = 18000
})
data.Add(New DataModel() With {
.Make = "Jeep",
.Model = "Patriot",
.Price = 22000,
.Mileage = 1000
})
data.Add(New DataModel() With {
.Make = "Honda",
.Model = "Civic",
.Price = 19500,
.Mileage = 30000
})
' Add the ListCollectionView as a resource
Me.Resources.Add("sort_DataSource", New ListCollectionView(data))
End Sub
End Class
The following topics provide additional information related to this topic.