Category / Section
How to bind WPF DataGrid (SfDataGrid) with CollectionViewSource?
1 min read
You can bind CollectionViewSource instead of binding an IEnumerable collection in WPF DataGrid (SfDataGrid). You have to create the view from CollectionViewSource using GetDefaultView method like below code example.
XAML
<Syncfusion:SfDataGrid x:Name="datagrid" AutoGenerateColumns="False" ItemsSource="{Binding Employee}"> <Syncfusion:SfDataGrid.Columns> <Syncfusion:GridTextColumn MappingName="EmployeeName" /> <Syncfusion:GridTextColumn MappingName="EmployeeAge" /> <Syncfusion:GridTextColumn MappingName="EmployeeGender" /> <Syncfusion:GridComboBoxColumn DisplayMemberPath="Country" ItemsSource="{Binding ProductsView, Source={StaticResource country}}" MappingName="CountryCode" SelectedValuePath="CountryCode" /> <Syncfusion:GridCheckBoxColumn MappingName="Review" /> </Syncfusion:SfDataGrid.Columns> </Syncfusion:SfDataGrid>
C#
private void InitialiseProductOptionsView() { Employee = CollectionViewSource.GetDefaultView(GDCSource); ProductsView = CollectionViewSource.GetDefaultView(Country); }