Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
Hi
I have a winforms app I'm trying to port to WPF. In that I have a datagridview bound to a bindingsource and use
BindingSource.Filter = string.Format("Column LIKE '%{0}%'", tb_Filter.Text);

in a textchanged event method to let the user filter the datagrid in real time.

I'd like to do something similar in my wpf application.
When I bind the datagrid directly to a dataset, I can use the RowFilter statement on the table's DefaultView and it works, but it's really laggy with over 500 rows in the datagrid.

So I thought I'd try to bind it to and ICollectionView or ListCollectionView instead.

I'm gonna post most of the XAML for the window and relevant code.

XAML looks like this
XML
<Window x:Class="Test_DataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:local="clr-namespace:Test_DataGrid"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="531" Width="769">
    <Window.Resources>
        <ObjectDataProvider x:Key="ConDataProvider"
                            ObjectType="{x:Type local:ConDataProvider}"/>
        <ObjectDataProvider x:Key="con_FullGrid"
                            ObjectInstance="{StaticResource ConDataProvider}"
                            MethodName="GetIcol"/>
    </Window.Resources>
    <Grid Name="mainGrid" DataContext="{Binding Source={StaticResource con_FullGrid}}">
        <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" Height="423" HorizontalAlignment="Left" Margin="36,49,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="687" FrozenColumnCount="20" RowHeight="20">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=cID}" CanUserReorder="False" CanUserResize="False" CanUserSort="False" Header="" />
                <DataGridTextColumn Binding="{Binding Path=Nafn}" CanUserReorder="False" CanUserResize="False" CanUserSort="False" Header="" />
//some more column definitions here
            </DataGrid.Columns>
        </DataGrid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="147,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" TextChanged="textBox1_TextChanged" />
    </Grid>
</Window>


Here is the ConDataProvider class that the DataGrid is bound to. Gets the data into a dataset from a mysqldataadapter and puts it into either a ListCollectionView or an ICollectionView. Not sure which one I should use. Maybe neither?

class ConDataProvider
    {
        private MySqlDataAdapter adapter;
        private Data data;
        private ListCollectionView lcolview;
        private DataSet dataset;
        private ICollectionView ICol;

        public ConDataProvider()
        {
            dataset = new DataSet();
            data = new Data();
            adapter = data.getAdapter("SELECT * FROM con_FullGrid");
            adapter.Fill(dataset, "con_FullGrid");

        }
        public ListCollectionView GetView()
        {
            lcolview = new ListCollectionView(dataset.Tables["con_FullGrid"].DefaultView);
            return lcolview;
        }

        public ICollectionView GetIcol()
        {
            ICol = CollectionViewSource.GetDefaultView(dataset.Tables["con_FullGrid"].DefaultView);
            return ICol;
        }
        
    }


Now I'm not sure where I'd go from here.
Can I apply a filter to the same instance of the ConDataProvider that is used in XAML and how would I filter the collection in a similar way as I did with the bindingsource?

Also I've been wondering how I can keep the datagrid in sync with the mysql database. It's gonna be read only, but I'd like it to update whenever data is updated in the database.

I've been trying to figure this out for hours, but all I've found about this is really confusing to me at the moment :)
Hoping someone could point me in the right direction.

Thanks in advance.
Posted

1 solution

I don't know how helpful this is going to be to you, since you seem to already have the basics, but I found this article[^] yesterday.
 
Share this answer
 
Comments
Tarun.K.S 6-Mar-11 12:14pm    
Good link!
steini1441 6-Mar-11 16:18pm    
I'm trying to apply this stuff to my project but it's going too well. His datasource is way different from mine, I'm not sure how to make his filter method work with my code.
Also I've read through every tutorial I've found on google but mostly I'm just suffering a severe case of brain block :(
It would really help to get an example that could work in my app.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900