【问题标题】:How to load a datatable into wpf data grid如何将数据表加载到 wpf 数据网格中
【发布时间】:2010-11-09 13:45:37
【问题描述】:

我正在尝试在我的 wpf 数据网格中显示数据表的内容

在下面的帖子中:

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

我找到了代码:

myDataGrid.ItemsSource = myDataTable.DefaultView;

而且行为很奇怪:它显示了 n 个空行,其中 n 是正确的:查询结果为 n 行!

那为什么我看不到它们呢?

【问题讨论】:

  • DataGrid 是否自动生成列?
  • +1 是的,我已经找到了... myGrid.AutoGenerateColumns = true;这确实是问题所在。

标签: wpf datagrid


【解决方案1】:

如果没有看到代码,我猜你没有将AutoGenerateColumns 设置为true。这很好,但如果您不自动生成列,则需要手动将列定义添加到数据网格中。

【讨论】:

    【解决方案2】:

    一种可能的解决方案是将DataGrid 替换为DataGridView 并将DataGridView.DataSource 属性绑定到DataTable 本身。根据this example

    dataGrid1.DataSource = table;

    【讨论】:

    • 我们正在谈论 WPF。 DataGridView 是一个 WinForms 控件。
    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 2011-01-31
    • 2017-05-14
    • 2014-06-02
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多