I have List<List<double>> with values and wpf datagrid.
How can I set this as dataSource to my dataGrid?
I have tried following:
public class DataContainer
{
public List<List<double>> List { get; set; }
public List<string> Headers { get; set; }
}
private void InitializeGrid(DataContainer container)
{
var table = new DataTable();
foreach (var header in container.Headers)
{
dataGrid1.Columns.Add(new DataGridTextColumn(){Header = header});
table.Columns.Add(header);
}
foreach (var lst in container.List)
{
var dr = table.NewRow();
var array = (from o in lst
select (object)o).ToArray();
dr.ItemArray = array;
table.Rows.Add(dr);
}
foreach (var row in table.Rows)
{
dataGrid1.Items.Add(row);
}
// dataGrid1.ItemsSource = table.Rows;
}
And this only add headers and empty rows.
You can go two directions but you need to pick one.
One:
Create the DataTable (not the columns). Use the headers to name the columns in the DataTable. Bind the DataTable with autogenerate columns.
Two:
Do NOT create the DataTable. Bind to List (using List as a property name is a bad practice and confusing). Then you you bind the column content to something like List[0], List[1]. I am not sure what the syntax is as I have done List where MyClass has a public List MyRows and then the syntax for the content binding is MyRows[0], MyRows[1] ….