Back to Devexpress

Bind the WPF Data Grid to ICollectionView

wpf-11124-controls-and-libraries-data-grid-bind-to-data-bind-to-icollectionview.md

latest4.1 KB
Original Source

Bind the WPF Data Grid to ICollectionView

  • Jan 09, 2023
  • 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.

Run Demo: CollectionView

xaml
<dxg:GridControl x:Name="grid" ItemsSource="{Binding CollectionView}">
    <dxg:GridControl.View>
        <dxg:TableView IsSynchronizedWithCurrentItem="True">/>
    </dxg:GridControl.View>
</dxg:GridControl>
csharp
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();
    }
}
vb
Public Class CollectionViewViewModel
    Inherits BindableBase

    Private employeesCore As IList = EmployeesWithPhotoData.DataSource

    Public ReadOnly Property Employees As IList
        Get
            Return employeesCore
        End Get
    End Property

    Public Property CollectionView As ICollectionView

    Public Sub New()
        CollectionView = New CollectionViewSource() With {
            .Source = Employees
        }.View
        CollectionView.GroupDescriptions.Add(New PropertyGroupDescription("JobTitle"))
        CollectionView.SortDescriptions.Add(New SortDescription("JobTitle", ListSortDirection.Ascending))
        CollectionView.MoveCurrentToFirst()
    End Sub
End Class

View Example: Synchronize the WPF Data Grid with the ICollectionView

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.