筛选绑定到DataTable WPF的Datagrid [英] Filtering Datagrid Binded to DataTable WPF

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

问题描述

我正在尝试为WPF中的DataGrid创建过滤系统,就像在链接中一样-

I am trying to create a filtering system for a DataGrid in WPF just as in the link -

http://www.codeproject.com/Articles/42227/Automatic-WPF-Toolkit-DataGrid-Filtering

我正在使用他们使用的同一库,但问题是我需要将DataGrid绑定到数据表...

I am using the same library that they are using but the thing is I need to bind my DataGrid to a datatable...

这里就是错误所在。.只要ItemSource是LIST,过滤器库就可以完美地工作,但是当ItemSource是DataTable时,过滤器库就停止工作...

And there is where the error is.. the filter library works perfectly as long as the ItemSource is a LIST but stops to work when the ItemSource is a DataTable...

有其他选择或建议吗???感谢工作示例。

Any Alternatives or Suggestions??? Working Examples appreciated..

我正在使用AutoColumnGeneration = True,因为我不知道需要填充多少列

推荐答案

表中的每个DataRow都包含一个平面对象数组,这些对象的类型只能通过从DataColumns数组中提取来访问。由于Array中的所有内容都是装箱的,因此依赖于强类型集合(如List of T)的库将不兼容。那应该可以解释为什么它不起作用。

Each DataRow in a table contains a flat Array of objects whose type is accessible only by extracting it from the DataColumns array. Since everything in the Array is boxed, a library that relies upon a strongly typed collection, like List of T will not be compatible. That should explain why it's not working.

您写道这很紧急,在一个融合度很短的项目中,我不得不做一次相同类型的事情,我将概述我的方法。这个答案仅适用于 URGENT 案例。

You wrote that it's urgent and I had to do the same type of thing once in a project with a very short fuse and I'll outline my approach. This answer is for URGENT cases only.

这里是一个ViewModel ...

Here's a ViewModel...

public class ViewModel : INotifyPropertyChanged
{
    public CollectionView MyCollectionView { get; set; }
    public ViewModel(DataTable dataTable)
    {
        MyCollectionView = CollectionViewSource.GetDefaultView(dataTable.Rows) as CollectionView;
        if (MyCollectionView != null)
        {
            MyCollectionView.Filter = o => (o as DataRow).ItemArray[0].ToString().Contains("2");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

构造函数获取一个DataTable并使用行作为CollectionView的绑定源。由于CollectionView具有内置的过滤和排序属性,因此它是用作绑定源的好工具。同样如图所示,构造函数为示例添加了一个过滤器以帮助您入门。

The Constructor takes a DataTable and uses the Rows as the binding source to a CollectionView. Since the CollectionView has built-in filtering and sorting properties, it's a great tool to use as a binding source. Also as shown, the Constructor adds a filter for an example to help get you started.

要将数据呈现给用户,请考虑以下Xaml ...

To present the data to the user, consider the following Xaml...

        <DataGrid ItemsSource="{Binding MyCollectionView}"
                  AutoGenerateColumns="False"
                  CanUserSortColumns="True"
                  IsReadOnly="True"
                  >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name"    Binding="{Binding ., Converter=
                       {db:DbConverter}, ConverterParameter=PersonName}"/>
                <DataGridTextColumn Header="Address"    Binding="{Binding ., Converter=
                        {db:DbConverter}, ConverterParameter=PersonAddress}"/>
            </DataGrid.Columns>
        </DataGrid>

这是一个DataGrid,其ItemsSource绑定到在ViewModel中声明和分配的CollectionView。每列数据与一个参数一起传递给转换器,该参数告诉转换器它需要做什么。

This is a DataGrid whose ItemsSource is bound to the CollectionView that was declared and assigned in the ViewModel. Each column of data is passed through a converter along with a parameter which tells the converter what it needs to do.

要关闭所有功能,下面是转换器...

To cap everything off, here's the converter...

public class DbConverter : MarkupExtension, IValueConverter
{
    private static readonly Dictionary<object, int> ParameterToColumnMapping;
    static DbConverter()
    {
        ParameterToColumnMapping = new Dictionary<object, int>
            {
                {"PersonName", 0}, {"PersonAddress", 1}
            };
    }
    public object Convert(object value, Type targetType, object parameter, 
                          System.Globalization.CultureInfo culture)
    {
        DataRow dr = value as DataRow;
        if (dr != null)
        {
            if (ParameterToColumnMapping.ContainsKey(parameter))
            {
                return dr.ItemArray[ParameterToColumnMapping[parameter]];
            }
        }
        return value;
    }
    public object ConvertBack(object value, Type targetType, object parameter, 
               System.Globalization.CultureInfo culture)
    {
        return null;
    }
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

这应该使您通过过滤和展示回到正轨当您有数据表和紧急情况时。全部编码大约需要一个小时。还请注意,我的绑定到 Rows ,但是如果需要,您也可以绑定到 DefaultView

That should get you back on track with filtering and presenting when you have a DataTable and an urgent situation. All in the coding takes about an hour. Note also that mine binds to the Rows, but you can also bind to the DefaultView if needs be.

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

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