views:

150

answers:

3

How can I get .All() method's result as a DataTable ?

Currently it returns IQueryable, wich can't be used as datasource for the WinForms DataGridView control.

dataGridView1.DataSource = Product.All(); // not working

A: 

Unless your Product class implements one of the following: IList, IListSource, IBindingList, IBindingListView; you won't be able to bind the result to your DataGridView.

mamoo
+1  A: 

You can bind a List to a DataGridView control so just use the ToList() method on the IQueryable e.g.

MyDataGridView.DataSource = MyObject.All().ToList();
Adam
A: 

For two way binding you can use a BindingList

dataGridView1.DataSource = new BindingList<Product>(Product.All().ToList());

The BindingList will update automatically when you add/remove rows from the DataGridView and the DataGridView will update automatically whan you add items to the binding list.

If you want to automatically update the DataGridView when modifying a Product, Product must implement INotifyPropertyChaged

Catalin DICU