views:

2203

answers:

2

How to filter my datagridview by the value of my label.text on click event? That value is from my linq query:

dataSet.Tables[0].AsEnumerable().Where(c => c.Field<int>("ageColumn") > 3 &&
     c.Field<int>("ageColumn") < 5).Count();

Let's just say the above query gives me 12 (label.text = 12), now when I click "12", I want my datagridview to ONLY show those 12 rows that meet my above query.

A: 

Now I do not use LINQ, but logic would suggest that whatever is retured by the expression

dataSet.Tables[0].AsEnumerable().Where(c => c.Field<int>("ageColumn") > 3 &&
 c.Field<int>("ageColumn") < 5)

Contains the data you seek? Is there not a property in there to enumerate the data?

Rob Cooper
A: 

Do you need it to be dynamic? Perhaps store the query itself as a lambda in the Tag property of your label:

Predicate<DataColumn> clause = c => c.Field<int>("ageColumn") > 3 
    && c.Field<int>("ageColumn") < 5;
label1.Tag = clause;

... then re-evaluate your query when the label is clicked:

var clause = (sender as Label).Tag as Predicate<DataColumn>; 
myDataSource = dataSet.Tables[0].AsEnumerable().Where(clause);

I don't know if this would work, but at least it would let you "attach" a where-clause to various lables.

I'd also recommend taking a look at Bindable LINQ so the results of your queries can be bound to. Very cool stuff.

Matt Hamilton