tags:

views:

6994

answers:

5

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 the WPFToolkit DataGrid?

+1  A: 

Hi Edward

You'll have to project your datatable into something that implements IEnumerable as that is waht the DataGrid expects. The grid is a different implementation to the Telerik version so its hard to expect the same functionality from both.

Ray Booysen
This worked, in fact I was converting from an List<Contact> to the Datatable for the Telerik control so this is even easier, I just use the original List<Contact> object now, thanks.
Edward Tanguay
+4  A: 

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

HTH, Kent

Kent Boogaart
I found this: http://msdn.microsoft.com/en-us/library/aa325664(VS.71).aspx but how do I use the implementation of IListSource on DataTable in order to call the GetList() method?
Edward Tanguay
Updated my post.
Kent Boogaart
+13  A: 

I find the easiest way is:

myDataGrid.ItemsSource = myDataTable.DefaultView;

because DataView implements IEnumerable

viggity
+1, binding to a DataView is simple and works great, especially if you want to be able to navigate relations.
Oskar
A: 

Hello This is Aizaz, I am developing a Network Application named Protocol Inspector and had plan to develop around 32 applications in WPF but It is looking difficult to develop a single, because Microsoft is right now focusing on Silverlight (datagrid controls etc) and they are not paying attention towards WPF they will provide support in future.

The worst thing with Microsoft technologies is that next release it totally up side down of the previous release they change every thing, I don't know why ... so there is always a huge learning curve in learning new Microsoft technologies. Where as Java got a well managed Documentation where Microsoft lacks and they take previous things with them in next release so it is not hard to learn new technologies.

Aizaz
Is this an answer or a whinge?
PandaWood
A: 

You could not bind datatable like this else you will have to tell in XAML like this

theGrid.ItemsSource = dt;

Faran Shabbir