如何将DataTable绑定到Datagrid [英] How to bind DataTable to Datagrid

查看:40
本文介绍了如何将DataTable绑定到Datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 DataTable 绑定到 DataGrid 时遇到问题.我已经搜索了解决方案,但无法摆脱错误.在使用 WindowsForms 时绑定工作正常,所以 DataTable 是正确的.我只是无法将它绑定到 WPF-DataGrid.

I have a problem binding a DataTable to a DataGrid. I have already searched for solutions but just can't get rid of the error. The binding works fine when using WindowsForms, so the DataTable is correct. I just can't bind it to a WPF-DataGrid.

错误消息: AmbiguousMatchException 未得到处理
来源:mscorlib

Error message: AmbiguousMatchException was unhandled
Source: mscorlib

我已经建立了新项目以去除任何不良链接等

I have already set up new project to get rid of any bad links, etc.

XAML 代码:

<DataGrid x:Name="grid1" Margin="10" ItemsSource="{Binding}" 
          AutoGenerateColumns="True"></DataGrid>

我已经尝试过后面的 C#-Code:

grid1.ItemsSource = dt.DefaultView;

grid1.DataContext = dt.DefaultView;

grid1.DataContext = dtex;

感谢任何帮助.

推荐答案

在 cs 文件中

DataTable employeeData = CreateDataTable();
gridEmployees.DataContext = employeeData.DefaultView;

在 xaml 文件中

<DataGrid Name="gridEmployees" ItemsSource="{Binding}">

这篇关于如何将DataTable绑定到Datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆