281
281

Reputation: 59

Filter DataGrid using a ComboBox

I am trying to filter my data in a DataGrid using a ComboBox.

I have this in the xaml :

    <ComboBox x:Name="cmbFilter" SelectionChanged="cmbFilter_SelectionChanged" />
       <Grid>
             <DataGrid x:Name="dataList">
                   <DataGrid.Columns >
                       <DataGridTextColumn Header="School" Binding="{Binding SchoolName}"></DataGridTextColumn>
                       <DataGridTextColumn Header="Category" Binding="{Binding CategorySchool}"></DataGridTextColumn>
                   </DataGrid.Columns>
             </DataGrid>
       </Grid>

And in the code behind :

//fill the list with the datas
this.dataList.ItemsSource = MainWindow._RE.ListDatas;

//fill the combobox with the school names
this.cmbFilter.ItemsSource = MainWindow._RE.ListNameSchool;

private void cmbFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // ??????
}

I manage to fill the DataGrid with all the datas et the ComboBox with all the school names. What I want is to be able to filter the Datagrid depending on which School name is selected from the ComboBox with the column "School". That the data displayed are only those from the school selected in the ComboBox

Thank you

Upvotes: 0

Views: 959

Answers (1)

Roman
Roman

Reputation: 12201

You can use Where():

private void cmbFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    this.dataList.ItemsSource = MainWindow._RE.ListDatas.Where(i => i.SchoolName == (string)cmbFilter.SelectedItem);
}

Upvotes: 1

Related Questions