I working on a C# win-form app.
in my form i have Data-Grid-View that bind with a Binding-Source and Binding-Source Data-source is a table of entity-framework.
when i use entity framework i cant use filter of the binding-source and the sort by columns of data-grid-view but when i don't use entity framework i can use them. how can i use them with entity-framework?
my code when form loads is
contex = new myEntity();
myBindingSource.DataSource = contex.myTable;
Every BindingSource has a property SupportsFiltering (and SupportsSorting). In case you set an entitySet as DataSource these two properties are automatically set to false! So there are some possible solutions. Either you provide an extension method to your IEnumberable<T>
as shown here or you choose to filter/sort using Linq2Entities
.
Snippet for Linq2Entities could be:
myEntity context = new myEntity();
myBindingSource.DataSource = (from m in context.myTable
where m.PROPERTY MEETS CONDITION
select m).ToList<TYPE>();
Snippet for extension method:
// extension method
public static IEnumerable<T> Filter<T>(this IEnumerable<T> list, Func<T, bool> filterParam) {
return list.Where(filterParam);
}
// filtering example
context.myTable.Filter(x => x.ItemName.StartsWith("Test"))
Furthermore this subject has been discussed many times before, even here on SO.
You tagged Entity framework, but you didn't show your model. Assuming that MyEntity is your ObjectContext or DBContext you could bind to the local property of the DbSet which is an observable collection that supports sorting.
myBindingSource.DataSource = contex.myTable.Local;