使用 C# 对 WPF 数据网格应用多重过滤
我有一个与数据库表相连的数据网格。我可以使用下面的代码使用 ICollectionView 过滤数据网格:
lstOrdsRlsd = new ObservableCollection<OrdsRlsd>(GV.dbContext.OrdsRlsds);
view = CollectionViewSource.GetDefaultView(lstOrdsRlsd);
if (lstOrdsRlsd.Count > 0)
{
dgRecords1.ItemsSource = view;
}
private void FilterRecords(string FieldName, string Condition, object Value1, object Value2)
{
OrdsRlsd vitem;
switch (FieldName)
{
case "OrderNo":
view.Filter = item =>
{
vitem = item as OrdsRlsd;
switch (Condition)
{
case "=":
if (vitem.OrderNo == Convert.ToDouble(Value1))
return true;
break;
case ">=":
if (vitem.OrderNo >= Convert.ToDouble(Value1))
return true;
break;
case "<=":
if (vitem.OrderNo <= Convert.ToDouble(Value1))
return true;
break;
case "between":
if (vitem.OrderNo >= Convert.ToDouble(Value1) && vitem.OrderNo <= Convert.ToDouble(Value2))
return true;
break;
}
return false;
};
break;
case "DateRqd1":
view.Filter = item =>
{
vitem = item as OrdsRlsd;
switch (Condition)
{
case "=":
if (vitem.DateRqd1 == Convert.ToDateTime(Value1))
return true;
break;
case ">=":
if (vitem.DateRqd1 >= Convert.ToDateTime(Value1))
return true;
break;
case "<=":
if (vitem.DateRqd1 <= Convert.ToDateTime(Value1))
return true;
break;
case "between":
if (vitem.DateRqd1 >= Convert.ToDateTime(Value1) && vitem.DateRqd1 <= Convert.ToDateTime(Value2))
return true;
break;
}
return false;
};
break;
}
当只有一个过滤条件时,它对我有用。但是当我应用多个条件时,数据网格会按最后一个条件进行过滤。这是用户单击过滤器按钮时执行的代码。
private void BtnFilter_Click(object sender, RoutedEventArgs e)
{
foreach (var rec in lstClsFilterGrid)
{
FilterRecords(rec.FieldName, rec.Condition, rec.Value1, rec.Value2);
}
}
其中 lstClsFilterGrid 是具有过滤条件的类对象的列表。
我附上了我的表单的屏幕截图。
I have a datagrid which is boud to a database table. I could filter the datagrid using an ICollectionView using the code below:
lstOrdsRlsd = new ObservableCollection<OrdsRlsd>(GV.dbContext.OrdsRlsds);
view = CollectionViewSource.GetDefaultView(lstOrdsRlsd);
if (lstOrdsRlsd.Count > 0)
{
dgRecords1.ItemsSource = view;
}
private void FilterRecords(string FieldName, string Condition, object Value1, object Value2)
{
OrdsRlsd vitem;
switch (FieldName)
{
case "OrderNo":
view.Filter = item =>
{
vitem = item as OrdsRlsd;
switch (Condition)
{
case "=":
if (vitem.OrderNo == Convert.ToDouble(Value1))
return true;
break;
case ">=":
if (vitem.OrderNo >= Convert.ToDouble(Value1))
return true;
break;
case "<=":
if (vitem.OrderNo <= Convert.ToDouble(Value1))
return true;
break;
case "between":
if (vitem.OrderNo >= Convert.ToDouble(Value1) && vitem.OrderNo <= Convert.ToDouble(Value2))
return true;
break;
}
return false;
};
break;
case "DateRqd1":
view.Filter = item =>
{
vitem = item as OrdsRlsd;
switch (Condition)
{
case "=":
if (vitem.DateRqd1 == Convert.ToDateTime(Value1))
return true;
break;
case ">=":
if (vitem.DateRqd1 >= Convert.ToDateTime(Value1))
return true;
break;
case "<=":
if (vitem.DateRqd1 <= Convert.ToDateTime(Value1))
return true;
break;
case "between":
if (vitem.DateRqd1 >= Convert.ToDateTime(Value1) && vitem.DateRqd1 <= Convert.ToDateTime(Value2))
return true;
break;
}
return false;
};
break;
}
It works for me when there is only one filtering condition. But when i apply multiple conditions the datagrid filters by the last condition. This is the code executed when the user clicks the filter button.
private void BtnFilter_Click(object sender, RoutedEventArgs e)
{
foreach (var rec in lstClsFilterGrid)
{
FilterRecords(rec.FieldName, rec.Condition, rec.Value1, rec.Value2);
}
}
where lstClsFilterGrid is a llist of class objects with the filter conditions.
I am attaching a screenshot of my form.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,此表单上的工作流程是,用户创建一个条件进行过滤,并且可以选择过滤集合或添加另一个条件,对吗?
如果是这样,那么您将必须能够传递条件集合(可能传递给重载的过滤器方法)。
我之前已经过滤了多个(固定)条件(即绑定到映射到预先安排的条件的属性的几个复选框,例如“过去 30 天”、“已完成的订单”、“逾期发货”等)。
我的过滤器将根据所有这些属性检查项目。就您而言,您有不确定数量的动态条件。
如果是我,我会创建一个列表,向其中添加条件,然后将整个集合传递到过滤方法中,然后每个对象必须满足 for 循环中的所有条件才能返回 true。
So the workflow on this form is, User creates a condition to filter on and can optionally filter the collection or add another condition, is that correct?
If so, then you will have to be able to pass a collection of conditions (probably to an overloaded filter method).
I have previously filtered on multiple (fixed) conditions (i.e. several checkboxes bound to properties that map to pre-arranged conditions, say "Past 30 days", "Completed Orders", "Overdue Shipments", etc.).
My filter would check the items based on all of those properties. In your case, you have an indeterminate number of dynamic conditions.
If this were me, I would have a list to which I would add conditions, then pass the whole collection into a filtering method then each object would have to satisfy all of the conditions in a for loop to return true.