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

不打扰是莪最后的温柔 提交于 2019-12-21 04:27:11

问题


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?


回答1:


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.




回答2:


I find the easiest way is:

myDataGrid.ItemsSource = myDataTable.DefaultView;

because DefaultView implements IEnumerable




回答3:


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



回答4:


In such cases I bind ItemsSource to DataContex in XAML i.e.

ItemsSource={Binding} 

and then in codebehind I do

theGrid.DataContext = dt

This will help.



来源:https://stackoverflow.com/questions/502667/why-cant-i-bind-the-wpftoolkit-datagrid-itemsource-to-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!