DataGridView RowFiltering

E

eps

Hi

I have a datagridview in a winforms app that is bound to a binding
source which is in turn bound to a dataview of a datatable.

Basically I am trying to rowfilter on a boolean field in the table but
it doesn't seem to be working, no errors, it just displays everything
regardless of the value of boolean field.

I am not discounting schoolboy mistakes but I have checked all the
simple stuff I can think of (the name of the field exactly matches the
name of the field in the rowfilter for example).

Do I need to do anything after applying the rowfilter ?, I have tried
calling the Refresh() method on the datagridview but that did not make a
difference.

I can't post any code right now unfortunately, but I wondered if anyone
has done anything similar and could post an example.

Any help would be much appreciated.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Can you post some code?

Where are you applying the filtering?
 
E

eps

Ignacio said:
Hi,

Can you post some code?

Where are you applying the filtering?


I will try and post some code later.

I am applying the row filter to the defaultview of the datatable, the
binding source is bound to a property exposing of another class which
exposes the default view of the datatable.

I may be doing it in an odd way, I would like to see how to correctly do
it (yes i have googled, can't find an example involving a binding source).
 
F

Fredo

As I recall, there's a bug in the row filtering. It doesn't trigger an
update in the CurrencyManager.

The way I have handled this in the past is to create a class, let's call it
MyDataViewManager, that derives from DataViewManager. Then just add the
following method:


public void RefreshList()
{
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}

Then, instead of binding a dataset, do this:

grid.DataSource = new MyDataViewManager(myDataSet);

Then do everything else as you normally would. After you set the filter
string, then call:

(grid.DataSource as MyDataViewManager).RefreshList();

That will trigger the ListChanged event which will cause the grid to update.
 
R

RobinS

eps said:
I will try and post some code later.

I am applying the row filter to the defaultview of the datatable, the
binding source is bound to a property exposing of another class which
exposes the default view of the datatable.

I may be doing it in an odd way, I would like to see how to correctly do
it (yes i have googled, can't find an example involving a binding source).



If you are using a binding source to bind your datasource to the
datagridview, then you need to do the filtering off of the binding source.
This will filter the grid for you. It is this simple:

myBindingSource.Filter = string.Empty; //in case it's been filtered already
myBindingSource.Filter = ("TheFieldICareAbout = " + true);

When using a BindingSource, you should avoid messing with the underlying
data source.

RobinS.
GoldMail, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top