【问题标题】:Having a DataTable as a cell value of another DataTable in WPF将 DataTable 作为 WPF 中另一个 DataTable 的单元格值
【发布时间】:2022-08-24 20:54:50
【问题描述】:

所以我的问题是我想在 WPF 的另一个表中显示一个表。

我使用DataTable 来显示一些数据,并且有一列,我需要在其中显示另一个DataTable。我设置了AutoGenerateColumns=\"True\"。对于一些测试,这是我写的(嗯,它按预期工作):

  var curDataTable = new DataTable();
  curDataTable.Columns.Add(\"name\"  , typeof(string));
  curDataTable.Columns.Add(\"number\", typeof(int));

  DataRow curRowData = curDataTable.NewRow();
  curRowData[0] = \"jones\";
  curRowData[1] = 90;
  curDataTable.Rows.Add(curRowData);

现在,假设我已经有一个填充的DataTable _dataTable。我现在想在我的第二列中显示这个 _dataTable。这是我期望的工作,但什么不起作用:

  var curDataTable = new DataTable();
  curDataTable.Columns.Add(\"name\" , typeof(string));
  curDataTable.Columns.Add(\"table\", typeof(DataTable));

  DataRow curRowData = curDataTable.NewRow();
  curRowData[0] = \"jones\";
  curRowData[1] = _dataTable;
  curDataTable.Rows.Add(curRowData);

有谁知道如何解决这个问题?

  • 您不能使用DataTable 执行此操作。将DataTable 替换为具有属性的自定义类。
  • 问题是,我既不知道我的外部DataTable 的大小,也不知道我的内部DataTable 的大小 .... 你认为它是否适用于具有一个属性的自定义类,即@987654331 @?
  • 是的,或者一个数组或类似的东西。它不适用于嵌套的 DataTable

标签: c# wpf datagrid


【解决方案1】:

使用嵌套的 DataTables 将不起作用。

您应该做的是将DataTable 替换为自定义类型,并绑定到DataGridTemplateColumn 中此类型的自定义集合属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多