Binding Source Filtering in C #

My code is like this

BindingSource bs=new BindingSource();
List<Items> lstItems= ListItems();

bs.DataSource=lstItems;

I bind this bindingsouce to a gridview.

grd.DaataSource=bs;

Then I copy this original BindingSource to a separate BindngSource

BindingSource filterBs=new BindingSource();

Then I use the filter condition for Bindingsource

filterBs.Filter= "ItemCode='1' and cost>'200'";

grd.DataSource=null;

Rebind the grid snap to the filtered snap object.

grd.DataSource=filterBs;

But now it also shows all the entries in the grid.

+1
source share
3 answers

You probably ran into other problems using a list with a BindingSource and trying to filter, so I would recommend this stream SO DataBinding DataGridView and List <gt; with BindingSource .

+1
source

Then I copy this original BindingSource to a separate BindngSource

BindingSource filterBs=new BindingSource();

You are not copying it, you are creating a new BindingSource.

, - :

BindingSource filterBs = grd.DataSource;
+2

The binding source depends on the underlying data source to allow filtering and sorting. List<>does not support filtering, so yours BindingSourcedoes not support filtering. You can confirm if this is your problem by looking at the value bs.SupportsFilteringin the debugger immediately after the assignment bs.DataSource=lstItems;.

BindingSource.SupportsFiltering Property

If the list is not an IBindingListView, SupportsFiltering always returns false.

The only class I know about this implements IBindingListView:DataView

+2
source

Source: https://habr.com/ru/post/1648370/


All Articles