[英]Viewing 2D array in WPF Form C#
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. 我有一个自动生成的2D数组,其大小可以在2x2到7x7之间,我想在表单上显示该数组。 I used double[,] as for my array.
我使用double [,]作为数组。 I tried to bind using datagrid (from create data bind Interface) but I couldn't locate my array to bind it with the datagrid.
我尝试使用datagrid进行绑定(来自创建数据绑定接口),但是无法找到将其绑定到datagrid的数组。 my 2d array only contains numbers, for example:
我的2d数组仅包含数字,例如:
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. 我想以我在WPF中构建的表单来显示它,但是我对WPF和这些东西还是陌生的。
Any suggestions? 有什么建议么?
Populate a DataTable
from the Array, and use it as the ItemsSource
of the DataGrid
. 从数组中填充一个
DataTable
,并将其用作DataGrid
的ItemsSource
。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.