开发者

Apply multiple Filtering to WPF datagrid using C#

开发者 https://www.devze.com 2023-04-07 10:15 出处:网络
I have a datagrid which is boud to a database table. I could filter the datagrid using an ICollectionView using the code below:

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 开发者_StackOverflow中文版= 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.

Apply multiple Filtering to WPF datagrid using C#


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消