Home > database >  Having a DataTable as a cell value of another DataTable in WPF
Having a DataTable as a cell value of another DataTable in WPF

Time:08-25

so my problem is that I want to display a table inside another table in WPF.

I use a DataTable in order to display some data and there is one column, in which I need to display another DataTable. I set AutoGenerateColumns="True". For a little testing, this is what I wrote (well, it works as expected):

  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);

Now, let's say I already have a filled DataTable _dataTable. I now want to display this _dataTable in my second column. This is what i would expect to work, but what does not work:

  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);

Has anyone an idea how to fix this?

CodePudding user response:

Using nested DataTables won't work.

What you should do is to replace the outer DataTable with a custom type and bind to a custom collection property of this type in a DataGridTemplateColumn.

CodePudding user response:

I don't think that nested DataTables are possible... What you can do is create new column in '_datatable' with the name of "name"

_datatable.Columns.Add("name" , typeof(string)).SetOrdinal(0);

Using SetOrdinal(0) you can make "name" column first column. Now you maybe able to add new columns to it.

I hope it helps

  • Related