Linq, Datagridviews and Bindingsources

S

Søren Reinke

Hi there.

I am trying to get to know LINQ in combination with a bindingsource and
a datagridview.

In the framework we have at work we have created a usercontrol to do
filtering via textboxes on a datagridview. Therefore we databind through
a bindingsource.

But when i load the data from the database via LINQ instead of through a
datatable, i can't seem to get the filter property to work on the
bindingsouce. I can set it but absolutly nothing happens.

Do i have to do something special to get it to work with LINQ, or is it
simple not possible ?

If you need some source, i'll gladly make an example and post it.

In words, what i do is.

The datagridview has the bindingsource as datasource,
I load data from the database via LINQ.
Then i set the columns on the datagridview to be those contained in the
data from the database (primary key, a name).
I set the database to the 'var' which contains the LINQ's request result.

If i run the program i get the data shown, and i can sort when clicking
on the columnheaders and all that.

But if i set the filter with something like:

bindingsource.Filter="Key>25";

Absolut nothing happens, it still shows rows in the datagrid where the
primary key 'Key' is lower or equal to 25.

What is important is that the '25' in the above example can be a number
coming from a user input in the program.

Hope you can help.

Best regards
Søren Reinke
www.Dykkeren.dk
 
W

William Stacey [C# MVP]

TMK, only collections that support IBindingListView can be used with Filter.
As a work around you can filter the Entity like:

this.localTimeEntryBindingSource.DataSource =
db.LocalTimeEntries.Where(i=>i.Posted==false);

hth

W

Hi there.

I am trying to get to know LINQ in combination with a bindingsource and
a datagridview.

In the framework we have at work we have created a usercontrol to do
filtering via textboxes on a datagridview. Therefore we databind through
a bindingsource.

But when i load the data from the database via LINQ instead of through a
datatable, i can't seem to get the filter property to work on the
bindingsouce. I can set it but absolutly nothing happens.

Do i have to do something special to get it to work with LINQ, or is it
simple not possible ?

If you need some source, i'll gladly make an example and post it.

In words, what i do is.

The datagridview has the bindingsource as datasource,
I load data from the database via LINQ.
Then i set the columns on the datagridview to be those contained in the
data from the database (primary key, a name).
I set the database to the 'var' which contains the LINQ's request result.

If i run the program i get the data shown, and i can sort when clicking
on the columnheaders and all that.

But if i set the filter with something like:

bindingsource.Filter="Key>25";

Absolut nothing happens, it still shows rows in the datagrid where the
primary key 'Key' is lower or equal to 25.

What is important is that the '25' in the above example can be a number
coming from a user input in the program.

Hope you can help.

Best regards
Søren Reinke
www.Dykkeren.dk
 
S

Søren Reinke

William said:
TMK, only collections that support IBindingListView can be used with
Filter. As a work around you can filter the Entity like:

this.localTimeEntryBindingSource.DataSource =
db.LocalTimeEntries.Where(i=>i.Posted==false);

hth

Thanks for the tip, ill try it tommorov :)

/Søren
 

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