c# - 将 DataTable 绑定(bind)到 DataGrid 显示空行

标签 c# wpf xaml datagrid datatable

我想将数据表绑定(bind)到 wpf 中的数据网格。此数据网格在右侧显示列标题、正确的行数,但行为空。

当我调试我的数据表时(使用那个:https://stackoverflow.com/a/18353683/4632606)我有一个正确的数据表:

--- DebugTable() ---
07/08/2014           | 
---------------------|-
0,0186               | 
0,035                | 
---------------------|-

我的数据网格有正确的标题“07/08/2014”和 2 个空行。

这是我的 DataGrid Xaml:

<DataGrid AutoGenerateColumns="true" ItemsSource="{Binding Table}"></DataGrid>

我错过了什么?

更新: 我在输出中看到了:

System.Windows.Data Error: 40 : BindingExpression path error: '07' property not found on 'object' ''DataRowView' (HashCode=32322646)'. BindingExpression:Path=07/08/2014; DataItem='DataRowView' (HashCode=32322646); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

可能是制作DataTable的时候出错了?

public DataTable Table { get { return this.GetTable(); } }

private DataTable GetTable()
{
    DataTable table = new DataTable();
    foreach (var controle in Controles)
    {
        table.Columns.Add(new DataColumn(controle.Date.ToShortDateString()));
    }
    DataRow rowMoyenne = table.NewRow();
    DataRow rowEtendue = table.NewRow();
    for (int i = 0; i < table.Columns.Count; i++)
    {
        DataColumn column = table.Columns[i];
        rowMoyenne[column] = Controles[i].Moyenne.ToString();
        rowEtendue[column] = Controles[i].Etendue.ToString();
    }
    table.Rows.Add(rowMoyenne);
    table.Rows.Add(rowEtendue);
    return table;
}

最佳答案

关于c# - 将 DataTable 绑定(bind)到 DataGrid 显示空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28873056/

相关文章:

c# - 如何在C#中单击按钮并打开窗口发送电子邮件

头部跟踪应用程序的 WPF 3-D 性能

c# - 忽略 IDataErrorInfo 通知

c# - 即使焦点更改后键盘也不会消失

.net - 如何在 BackgroundWorker 中访问 COM 对象?

wpf - 如何使用正确的 Windows 系统颜色?

C# Windows App 删除页面

c# - 对 C# 多维列表中的行进行排序和删除

c# - 与经典 TCP 套接字通信

c# - 如何使用数据触发器设置 WPF 行为属性