显示DataTable的值 [英] Show values of a DataTable
问题描述
我有一个 DataTable
正在创建:
DataTable datatable = new DataTable();
使用某些数据填充 DataTable
我试图在一个 DataGrid
中显示其数据:
After populating the DataTable
with some data, I'm trying to show its data in a DataGrid
:
dataGrid1.DataContext = datatable.DefaultView;
我的 DataGrid
具有此配置: p>
My DataGrid
has this configuration:
<DataGrid AutoGenerateColumns="False" Height="124" HorizontalAlignment="Left"
Margin="41,92,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="335"
DataContext="{Binding}" ColumnHeaderHeight="20" RowHeight="20" />
尽管我的 DataTable
有预期的数据它, DataGrid
是空的。
Although my DataTable
has the expected data in it, the DataGrid
is empty.
我确定我的代码有问题,但我找不到什么。如何使用WPF中的 DataTable
中的数据在 DataGrid
中正确显示数据?
I'm sure there's something wrong with my code, but I can't find what. How can I correctly show data in the DataGrid
using data from a DataTable
in WPF?
推荐答案
在您的 DataGrid
中使用 AutoGenerateColumns =False
code>您还应该使用< DataGrid.Columns>
来完全控制显示哪些列以及如何查看和编辑它们的数据。或者将 AutoGenerateColumns
设置为 True
,以便 DataGrid
将自动为您生成适当的列:
When you use AutoGenerateColumns="False"
in your DataGrid
you should also use <DataGrid.Columns>
to get full control of which columns are shown and how their data should be viewed and edited. Or set the AutoGenerateColumns
to True
so that the DataGrid
will automatically generate appropriate columns for you:
AutoGenerateColumns="True"
此外,您应该设置您的 DataGrid
的 ItemsSource
而不是 DataContext
:
Also you should set your DataGrid
's ItemsSource
instead of DataContext
:
dataGrid1.ItemsSource = datatable.DefaultView;
这篇关于显示DataTable的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!