为什么使用CollectionView过滤DataTable时出现错误? [英] Why errors when filters DataTable with CollectionView?

查看:346
本文介绍了为什么使用CollectionView过滤DataTable时出现错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我成功地将 DataTable 放入我的 CollectionView中使用:

  ICollectionView _collectionView {get;组; } 
public ICollectionView collectionView {......}

DataTable myDataTable = new DataTable();
myConnection.Open();
dbAdpater.Fill(myDataTable);
myConnection.Close();
var collectionList =(newLeadTable as IListSource).GetList();
this.collectionView = CollectionViewSource.GetDefaultView(collectionList);

我知道列表中的每个对象都是 DataRowView 和我迭代数据测试,我可以看到正确存储。



DataGrid

但是一旦我添加了Filter:

  this.collectionView.Filter = new Predicate< object>(
(obj)=>
{
return false; //仅用于测试建议
}
);

当我执行代码给我错误:


无法创建在程序集TestWPF,
Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null中定义的Window1的实例。异常有
被调用的目标抛出。错误标记文件
'TestWPF; component / Window1.xaml'第1行第9位。

输出:

b

lockquote
在PresentationFramework.dll中发生类型为System.NotSupportedException
的第一次机会异常
blockquote>

我还尝试将 DataTable 转换成自定义对象列表

所以我想知道当用 DataTable

解决方案

请参阅 http://msdn.microsoft.com/zh-CN/library/ms752347.aspx#binding_to_collections


为了提高性能,ADO.NET DataTable或DataView对象的集合视图将排序和筛选委托给DataView。



BindingListCollectionView
不支持筛选。相反,您必须使用 CustomFilter 属性,它接受一个过滤器字符串并将其用作基础 DataView.RowFilter 属性的值。字符串值是用来在SELECT查询中构造WHERE子句的SQL。

  this.collectionView.CustomFilter =价格> 20; 

如果要使用多个条件,则必须将它们与AND或OR像SQL)。


As titled.

So I was successfully to put DataTable into my CollectionView with using:

    ICollectionView _collectionView { get; set; }
    public ICollectionView collectionView {......}

            DataTable myDataTable= new DataTable();
            myConnection.Open();
            dbAdpater.Fill(myDataTable);
            myConnection.Close();
            var collectionList= (newLeadTable as IListSource).GetList();
            this.collectionView = CollectionViewSource.GetDefaultView(collectionList);

I know each of the object in the list is DataRowView and as I tested with iterating the data and I can see is stored properly.

The DataGrid was display properly when I execute the code.

But once I added the Filter:

            this.collectionView.Filter = new Predicate<object>(
                (obj) =>
                {
                    return false; //Just for testing propose
                }
            );

When I execute the code is giving me error:

Cannot create instance of 'Window1' defined in assembly 'TestWPF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error in markup file 'TestWPF;component/Window1.xaml' Line 1 Position 9.

And output:

A first chance exception of type 'System.NotSupportedException' occurred in PresentationFramework.dll

I also tried to stores converts the DataTable into a list of custom object for filtering, but is working fine.

So I wonder what did I done wrong when filtering with DataTable?

解决方案

See http://msdn.microsoft.com/en-us/library/ms752347.aspx#binding_to_collections

To improve performance, collection views for ADO.NET DataTable or DataView objects delegate sorting and filtering to the DataView.

The BindingListCollectionView does not support filtering. Instead, you have to use the CustomFilter property which takes a filter string and uses it as the value for the underlying DataView.RowFilter property. The string value is SQL that you would use to construct the WHERE clause in a SELECT query.

this.collectionView.CustomFilter = "Price > 20";

If you want to use multiple conditions, then you have to string them together with AND or OR (just like SQL).

这篇关于为什么使用CollectionView过滤DataTable时出现错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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