Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I bind the WPFToolkit DataGrid ItemSource to DataTable?

In a Telerik control, I was able to bind a DataTable directly to the ItemSource, but when I switched to the Codeplex WPFToolkit Datagrid:

<dg:DataGrid Name="theGrid"/>
---
theGrid.ItemsSource = dt;

I get this error:

Cannot implicitly convert type 'System.Data.DataTable' to 'System.Collections.IEnumerable'.

How can I bind the DataTable to theWPFToolkit DataGrid?

like image 673
Edward Tanguay Avatar asked Feb 02 '09 09:02

Edward Tanguay


2 Answers

I find the easiest way is:

myDataGrid.ItemsSource = myDataTable.DefaultView;

because DefaultView implements IEnumerable

like image 174
viggity Avatar answered Oct 13 '22 14:10

viggity


I'm assuming support for this will be added in the future, but for now you can use the implementation of IListSource on DataTable. Call the GetList() method and use that as your data source. It's an explicit interface implementation so you'll need to cast:

var data = (myDataTable as IListSource).GetList();
like image 24
Kent Boogaart Avatar answered Oct 13 '22 15:10

Kent Boogaart