筛选绑定到ICollectionView或ListCollectionView的DataGrid [英] Filtering a DataGrid bound to a ICollectionView or ListCollectionView

查看:208
本文介绍了筛选绑定到ICollectionView或ListCollectionView的DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个Winforms应用程序,我正在尝试移植到WPF.因为我有一个绑定到绑定源的datagridview并使用

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);


在textchanged事件方法中可以让用户实时过滤数据网格.

我想在我的wpf应用程序中做类似的事情.
当我将datagrid直接绑定到数据集时,我可以在表的DefaultView上使用RowFilter语句,它可以工作,但是在datagrid中有超过500行的确很滞后.

所以我想尝试将其绑定到ICollectionView或ListCollectionView.

我将为该窗口和相关代码发布大部分XAML.

XAML看起来像这样


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

<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>



这是DataGrid绑定到的ConDataProvider类.从mysqldataadapter获取数据到数据集中,并将其放入ListCollectionView或ICollectionView中.不知道我应该使用哪一个.也许都不是吗?



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;
        }
        
    }



现在我不确定该从哪里去.
是否可以将过滤器应用于XAML中使用的ConDataProvider的同一实例,以及如何以与绑定源类似的方式过滤集合?

我也一直想知道如何使datagrid与mysql数据库保持同步.它将是只读的,但是我希望它在数据库中的数据更新时进行更新.

我已经尝试了好几个小时了,但是目前我发现的一切真让我感到困惑:)
希望有人可以指出正确的方向.

预先感谢.



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.

推荐答案

我不知道这对您有多大帮助,因为您似乎已经具备基础知识,但是我发现本文 [
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.


这篇关于筛选绑定到ICollectionView或ListCollectionView的DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆