开发者

SubSonic 3. Get results as DataTable

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

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

dataGridView1.DataSource = Product.A开发者_如何学Goll(); // not working


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();


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜