4

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;
Arman
  • 1,019
  • 2
  • 14
  • 33
  • possible duplicate of [How to sort DataGridView when bound to a binding source that is linked to an EF4 Entity](http://stackoverflow.com/questions/4463505/how-to-sort-datagridview-when-bound-to-a-binding-source-that-is-linked-to-an-ef4) – David Hall Aug 25 '12 at 20:35

2 Answers2

4

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.

Community
  • 1
  • 1
Pilgerstorfer Franz
  • 8,303
  • 3
  • 41
  • 54
  • editet and updated answer. @calling saveChanges on bindingSourceChange - sry I don't understand this comment. What exactly do you mean with this, perhaps you could update your question according to this detail - thx in advance – Pilgerstorfer Franz Aug 25 '12 at 20:40
0

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;
Mark Oreta
  • 10,346
  • 1
  • 33
  • 36