Bind the WPF Data Grid to ICollectionView
- 2 minutes to read
The GridControl allows you to sort, group, and filter data at the control level. If you bind the grid to an ICollectionView, you can perform these data shaping operations at the data source level.
We recommend that you use ICollectionView in the following cases:
- You perform data shaping operations at the collection level and share these settings across multiple controls.
- Synchronize row selection across multiple controls.
- You manage multi-thread updates.
To bind the GridControl to ICollectionView, assign the source collection to the DataControlBase.ItemsSource property.
Set the DataViewBase.IsSynchronizedWithCurrentItem property to true to synchronize the focused row with the current item in ICollectionView.
<dxg:GridControl x:Name="grid" ItemsSource="{Binding CollectionView}">
<dxg:GridControl.View>
<dxg:TableView IsSynchronizedWithCurrentItem="True">/>
</dxg:GridControl.View>
</dxg:GridControl>
public class CollectionViewViewModel : BindableBase {
IList employeesCore = EmployeesWithPhotoData.DataSource;
public IList Employees { get { return employeesCore; } }
public ICollectionView CollectionView { get; private set; }
public CollectionViewViewModel() {
CollectionView = new CollectionViewSource() { Source = Employees }.View;
CollectionView.GroupDescriptions.Add(new PropertyGroupDescription("JobTitle"));
CollectionView.SortDescriptions.Add(new SortDescription("JobTitle", ListSortDirection.Ascending));
CollectionView.MoveCurrentToFirst();
}
}
#Limitations
If you bind the GridControl to ICollectionView, the control works in Server Mode. Refer to the following topic for information on Server Mode Limitations. To avoid these limitations, set AllowCollectionView to false. In this case, the GridControl performs data operations on its own.