Filter Datagrid base on Combobox selection(WPF)

developer2012
developer2012 used Ask the Experts™
on
Hi everyone,

How to filter data grid on combo box selection?

For example I am selecting a UID in combobox and click on view button then it should display all the records assoicated with it in the grid?

How do I achieve it?

THanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008

Commented:
How are you binding data to the DataGrid?

Author

Commented:
Through a Dataset
Most Valuable Expert 2012
Top Expert 2008

Commented:
Are you setting the ItemsSource for the DataGrid to the DataSet in code?
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Author

Commented:
I am doing it through XAML.
Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:datagrideg">
    <Window.Resources>
        <my:DataSet1 x:Key="DataSet1" />
        <CollectionViewSource x:Key="TblSystemInformationViewSource" Source="{Binding Path=tblSystemInformation, Source={StaticResource DataSet1}}" />
    </Window.Resources>
  
    
        <Grid DataContext="{StaticResource TblSystemInformationViewSource}">
          
        <ComboBox Height="39"  HorizontalAlignment="Left" Margin="133,43,0,0" Name="ComboBox1" VerticalAlignment="Top" Width="147" DisplayMemberPath="DOT" ItemsSource="{Binding}" SelectedValuePath="UID" IsSynchronizedWithCurrentItem="True"  />
        
        <DataGrid AutoGenerateColumns="False" Height="250" HorizontalAlignment="Left" Margin="88,111,0,0" Name="DataGrid1" VerticalAlignment="Top" Width="311" ItemsSource="{Binding}" >
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="UIDColumn" Binding="{Binding Path=UID}" Header=" UID" IsReadOnly="True" Width="Auto" />
                <DataGridTextColumn x:Name="DOTColumn" Binding="{Binding Path=DOT}" Header="DOT" Width="Auto" />
                <DataGridTextColumn x:Name="StreetNameColumn" Binding="{Binding Path=StreetName}" Header="Street Name" Width="Auto" />
                <DataGridTextColumn x:Name="CityColumn" Binding="{Binding Path=City}" Header="City" Width="Auto" />
                <DataGridTextColumn x:Name="State" Binding="{Binding Path=State}" Header="State" Width="Auto" />
            </DataGrid.Columns>
            </DataGrid>
    </Grid>
</Window>

Open in new window

Most Valuable Expert 2012
Top Expert 2008
Commented:
Since you are using the CollectionViewSource, that has the capability to filter.

CollectionViewSource.Filter Event
http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource.filter.aspx

Example:

private void ShowOnlyBargainsFilter(object sender, FilterEventArgs e)
{
    AuctionItem product = e.Item as AuctionItem;
    if (product != null)
    {
        // Filter out products with price 25 or above
        if (product.CurrentPrice < 25)
        {
            e.Accepted = true;
        }
        else
        {
            e.Accepted = false;
        }
    }
}

Open in new window

Author

Commented:
Just a general question . IS it a bad practice to use CollectionView?
Most Valuable Expert 2012
Top Expert 2008
Commented:
A CollectionView is a good wrapper for binding, since it provides different capability, like filtering.  Personally, I wouldn't use a DataSet, since it doesn't fit with ORM providers, such as Entity Framework or NHibernate.

Here is a little light reading about ObservableCollection, and the demo binds a ListBox to a CollectionView, which references an ObservableCollection.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial