I have an auto generated 2D array that can have sizes between 2x2 to 7x7, and I would like to show the array on the form. I used double[,] as for my array. I tried to bind using datagrid (from create data bind Interface) but I couldn't locate my array to bind it with the datagrid. my 2d array only contains numbers, for example:
double[,] myArr = {{1,2},{3,4}};
I want to show it on the form I'm building in WPF, but I'm new to WPF and to these stuff.
Any suggestions?
Populate a DataTable
from the Array, and use it as the ItemsSource
of the DataGrid
.
double[,] myArr = {{1,2},{3,4}};
private void PopulateDataGrid()
{
DataTable dataTable = new DataTable();
for (int j = 0; j < myArr.GetLength(1); j++)
dataTable.Columns.Add(new DataColumn("Column " + j.ToString()));
for (int i = 0; i < myArr.GetLength(0); i++)
{
var newRow = dataTable.NewRow();
for (int j = 0; j < myArr.GetLength(1); j++)
newRow["Column " + j.ToString()] = myArr[i, j];
dataTable.Rows.Add(newRow);
}
this.dataGrid1.ItemsSource = dataTable.DefaultView;
}