BindingList Filtering problem

Hobbyist_programmer 621 Reputation points
2024-07-30T18:01:08.47+00:00

Hallo All,

While searching for a method to filter bindinglist of objects, i came accross the following sample code provided by microsoft. When testing it i experienced two issues.

https://www.microsoft.com/en-us/download/details.aspx?id=17914

  1. After applying filter to the bindingsource original list contains only filtered objects, for example if i have 100 objects in the list and if only 2 objects meets filter creiteria then the original list after filtering contains only these 2 objects, so if i serealize my list my file contains only these 2 objects.
  2. Again it is due to the first reason, i can not apply another filter with another bindingsource since the original list doesnot have other objects since verything was cleared due to first filter from first bindingsource.

I am not sure if i am using it worng or is there a way to fix ? could anyone please have a look at it?

Thanks.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,781 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 54,401 Reputation points
    2024-07-30T18:23:30.0866667+00:00

    Please post us the code you're using. The link you gave is very old.

    I suspect that the data is "missing" because you're trying to get the list of items using the binding source, but you applied a filter to it so of course the data is gone otherwise it would show up. In general you should keep your original data around somewhere and pass that to the binding source and then apply the filter to the source. If you need access to the original data then go back to the data you originally used to bind the source. It doesn't have filtering so it is fine. Refer to the current docs for a better example of how to do this.


  2. Hongrui Yu-MSFT 2,075 Reputation points Microsoft Vendor
    2024-08-01T10:21:05.22+00:00

    Hi,@Hobbyist_programmer. Welcome to Microsoft Q&A. 

    For BindingSource

    The data bound to BindingSource.DataSource seems to be consistent with the data you pass, so when you add filter rules to BindingSource.Filter, the data you pass is also changed.

    After a series of operations, when you need to remove the filter rules and restore the data to its original state, you could call the RemoveFilter() method of BindingSource.

    Of course, you could pass a copy of the original data to BindingSource.DataSource, so that the filter rules will not affect the original data.

     

    For BindingList

    In the description of the following document, the difference between BindingList and ObservableCollection is not much.

    c# - Difference between ObservableCollection and BindingList - Stack Overflow

     

    BindingSource is the content of Winform. In WPF, ObservableCollection+CollectionViewSource is generally used for filtering. Setting Filter in CollectionViewSource will not affect the original data.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.