A
A
Alexey2016-03-23 22:57:31
WPF
Alexey, 2016-03-23 22:57:31

How to implement multiple data filter?

You need to filter the data in the CollectionViewSource in the Filter event.
In CollectionViewSource I have a collection of one class.
With the filter on 1 field there are no problems. But it was required to make the plural filter with such condition:
There are 7 fields in the table. For each field there is a button to enable the filter for this field.
Tell me how to implement a filter in such a way that when you turn on more than 1 filter, they overlap each other.
I will explain in the pictures what I did and how it should work.
There is a list of goods. All filters are off. We see everything. It is right.
7d2d46933ccb479b8701ac6294127ecf.png
Turn on the filter by the Name field. We see only products containing the fragment "tor" in the name. It is right.
436dea0887b447fa82b706c0383213e2.png
We turn on the filter by the Group2 field, wanting to see only those from the list of products containing TOP in the name that belong to the group with the text "Snacks". But we get a list containing "TOP" OR "Snacks". Not right.
We b7ded7980d7a402ea80070f02357292a.png
get I need that in this case and subsequent overlays, it would work according to I.
For those who wish, I will show the code that works now.

if (cgA != null)
            {
               //если хотя бы 1 фильр включен, то проверяем условия
                if (  this.dgGoodsAllF1.IsChecked.Value ||
                        this.dgGoodsAllF2.IsChecked.Value ||
                        this.dgGoodsAllF3.IsChecked.Value ||
                        this.dgGoodsAllF4.IsChecked.Value ||
                        this.dgGoodsAllF5.IsChecked.Value ||
                        this.dgGoodsAllF6.IsChecked.Value ||
                        this.dgGoodsAllF7.IsChecked.Value ||
                        this.dgGoodsAllF8.IsChecked.Value ||
                        this.dgGoodsAllF9.IsChecked.Value ||
                        this.cbOnLink.IsChecked.Value     ||
                        this.cbInMatrix.IsChecked.Value   ||
                        this.cbOnLink.IsChecked.Value)
               
                { 

                            if (
                                (this.dgGoodsAllF1.IsChecked == true && cgA.Code.ToString() == this.dgGoodsAllT1.Text)
                                || (this.dgGoodsAllF2.IsChecked == true && (cgA.NameLong.ToUpper().Contains(this.dgGoodsAllT2.Text.ToUpper())))
                                || (this.dgGoodsAllF3.IsChecked == true && (cgA.cgGroup2.NameLong.ToUpper().Contains(this.dgGoodsAllT3.Text.ToUpper())))
                                || (this.dgGoodsAllF4.IsChecked == true && (cgA.cgGroup3.NameLong.ToUpper().Contains(this.dgGoodsAllT4.Text.ToUpper())))
                                || (this.dgGoodsAllF5.IsChecked == true && (cgA.cgGroup4.NameLong.ToUpper().Contains(this.dgGoodsAllT5.Text.ToUpper())))
                                || (this.dgGoodsAllF6.IsChecked == true && (cgA.cgGroup5.NameLong.ToUpper().Contains(this.dgGoodsAllT6.Text.ToUpper())))
                                || (this.dgGoodsAllF7.IsChecked == true && (cgA.cgGroup6.NameLong.ToUpper().Contains(this.dgGoodsAllT7.Text.ToUpper())))
                                || (this.dgGoodsAllF8.IsChecked == true && (cgA.cgGroup7.NameLong.ToUpper().Contains(this.dgGoodsAllT8.Text.ToUpper())))
                                || (this.dgGoodsAllF9.IsChecked == true && (cgA.cgGroup8.NameLong.ToUpper().Contains(this.dgGoodsAllT9.Text.ToUpper())))
                                || (this.cbOnLink.IsChecked == true && (cgA.Group5GUID == null))
                                || (this.cbInMatrix.IsChecked == true && (cgA.InMatrix == true))
                                )
                    
                                    e.Accepted = true;
                            else
                                e.Accepted = false;
                       
                }
                else   e.Accepted = true;
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2016-03-24
@k1lex

I solved the problem myself by checking the conditions one by one.

private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
        {
            cgAssortment cgA = e.Item as cgAssortment;
            bool isAccept = true;

            if (cgA != null)
            {
                try
                {
                    isAccept = isAccept && (!this.cbOnLink.IsChecked.Value || (this.cbOnLink.IsChecked == true && (cgA.Group5GUID == null)));
                    isAccept = isAccept && (!this.cbInMatrix.IsChecked.Value || (this.cbInMatrix.IsChecked == true && (cgA.InMatrix == true)));
                    isAccept = isAccept && (!this.dgGoodsAllF1.IsChecked.Value || (this.dgGoodsAllF1.IsChecked.Value && cgA.Code.ToString() == this.dgGoodsAllT1.Text));
                    isAccept = isAccept && (!this.dgGoodsAllF2.IsChecked.Value || (this.dgGoodsAllF2.IsChecked.Value && (cgA.NameLong.ToUpper().Contains(this.dgGoodsAllT2.Text.ToUpper()))));
                    isAccept = isAccept && (!this.dgGoodsAllF3.IsChecked.Value || (this.dgGoodsAllF3.IsChecked.Value && (cgA.cgGroup2.NameLong.ToUpper().Contains(this.dgGoodsAllT3.Text.ToUpper()))));
                    isAccept = isAccept && (!this.dgGoodsAllF4.IsChecked.Value || (this.dgGoodsAllF4.IsChecked.Value && cgA.cgGroup3 != null && (cgA.cgGroup3.NameLong.ToUpper().Contains(this.dgGoodsAllT4.Text.ToUpper()))));
                    isAccept = isAccept && (!this.dgGoodsAllF5.IsChecked.Value || (this.dgGoodsAllF5.IsChecked.Value && cgA.cgGroup4 != null && (cgA.cgGroup4.NameLong.ToUpper().Contains(this.dgGoodsAllT5.Text.ToUpper()))));
                    isAccept = isAccept && (!this.dgGoodsAllF6.IsChecked.Value || (this.dgGoodsAllF6.IsChecked.Value && cgA.cgGroup5 != null && (cgA.cgGroup5.NameLong.ToUpper().Contains(this.dgGoodsAllT6.Text.ToUpper()))));
                    isAccept = isAccept && (!this.dgGoodsAllF7.IsChecked.Value || (this.dgGoodsAllF7.IsChecked.Value && cgA.cgGroup6 != null && (cgA.cgGroup6.NameLong.ToUpper().Contains(this.dgGoodsAllT7.Text.ToUpper()))));
                    isAccept = isAccept && (!this.dgGoodsAllF8.IsChecked.Value || (this.dgGoodsAllF8.IsChecked.Value && cgA.cgGroup7 != null && (cgA.cgGroup7.NameLong.ToUpper().Contains(this.dgGoodsAllT8.Text.ToUpper()))));
                    isAccept = isAccept && (!this.dgGoodsAllF9.IsChecked.Value || (this.dgGoodsAllF9.IsChecked.Value && cgA.cgGroup8 != null && (cgA.cgGroup8.NameLong.ToUpper().Contains(this.dgGoodsAllT9.Text.ToUpper()))));
                }
                catch (Exception er )
                {
                    MessageBox.Show(er.Message);
                }
                e.Accepted = isAccept;
            }
        }

PS Other suggestions or explanations on how best to do it will only be happy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question