如何将数据从XML文件加载到DataGridView,如下表所示:
y\x [0,0] 0.000976563 0.000976563 0.000976563 0.000976563 0.000976563
y\x [10,10] 0.000976563 0.000976563 0.000976563 0.000976563 0.000976563
y\x [20,20] 0.000976563 0.000976563 0.000976563 0.000976563 0.000976563
y\x [30,30] 0.000976563 0.000976563 0.000976563 0.000976563 0.000976563
y\x [40,40] 0.000976563 0.000976563 0.000976563 0.000976563 0.000976563
我的XML文件的结构是:
<?xml version="1.0" standalone="yes"?>
<Tag>
<Parameter>
<Name>Parameter1</Name>
<Size_X>7</Size_X>
<Size_Y>2</Size_Y>
<Value_X>0;2;8;12;14;16;19;</Value_X>
<Value_Y>-20;-10;</Value_Y>
<Value>0000000000000;5.0000000000000000;10.0000000000000000;15.0000000000000000;20.0000000000000000;25.0000000000000000;0000000000000;5.0000000000000000;10.0000000000000000;15.0000000000000000;20.0000000000000000;25.0000000000000000;</Value>
</Parameter1>
<Parameter2>
<Name>Parameter2</Name>
<Size_X>7</Size_X>
<Size_Y>3</Size_Y>
<Value_X>0;2;4;6;8;10;12;</Value_X>
<Value_Y>20;40;60;</Value_Y>
<Value>0000000000000;5.0000000000000000;10.0000000000000000;15.0000000000000000;20.0000000000000000;30.0000000000000000; 0000000000000;5.0000000000000000;10.0000000000000000;15.0000000000000000;20.0000000000000000;30.0000000000000000;0000000000000;5.0000000000000000;10.0000000000000000;15.0000000000000000;20.0000000000000000;30.0000000000000000;</Value>
</Parameter2>
</Tag>
我已经尝试了下一个代码,但它不起作用:
XDocument xmlDoc = XDocument.Load("C:\\Write.xml");
var variables = from variable in xmlDoc.Descendants("Parameter")
where variable.Element("Name").Value == "Parameter1"
select new
{
Name = variable.Element("Name").Value,
SizeX = variable.Element("Size_X").Value,
SizeY = variable.Element("Size_Y").Value,
ValueX = variable.Element("Value_X").Value,
ValueY = variable.Element("Value_Y").Value,
Value = variable.Element("Value").Value,
};
foreach (var variable in variables)
{
var x = Convert.ToInt32(variable.SizeX);
var y = Convert.ToInt32(variable.SizeY);
dataGridView1.TopLeftHeaderCell.Value = "y/x";
var arr = new double[y, x];//{Value} it must be the value here???
var columnCount = arr.GetUpperBound(1) + 1;
var rowCount = arr.GetUpperBound(0) + 1;
for (int i = 0; i < columnCount; i++)
{
dataGridView1.Columns.Add(i.ToString(), variable.ValueX);
}
for (int i = 0; i < rowCount; i++)
{
dataGridView1.Rows.Add(i.ToString(),variable.ValueY);
for (int k = 0; k < columnCount; k++)
{
dataGridView1.Rows[i].Cells[k].Value = arr[i, k];
}
}
答案 0 :(得分:1)
首先添加列和行,然后填充单元格
var arr = new int[5, 3]; // array is just for example
var columnCount = arr.GetUpperBound(1)+1;
var rowCount = arr.GetUpperBound(0)+1;
for (int i = 0; i < columnCount; i++)
{
dataGridView1.Columns.Add(i.ToString()," ");
}
for (int i = 0; i < rowCount; i++)
{
dataGridView1.Rows.AddCopy(0);
for (int k = 0; k < columnCount; k++)
{
dataGridView1.Rows[i].Cells[k].Value = arr[i, k];
}
}
答案 1 :(得分:0)
本文Binding a two dimensional array to a DataGrid将为您提供一些见解。 基本上是:
//
// 1. Create two dimensional array
//
const int dim = 1000;
double[,] array = new double[dim,dim];
Random ran = new Random();
for(int r = 0; r < dim; r++)
{
for(int c = 0; c < dim; c++)
{
array[r,c] = (ran.Next(dim)); // fill it with random numbers.
}
}
// 2. Create ArrayDataView class in which
// constructor you pass the array
// and assign it to DataSource property of DataGrid.
dataGrid1.DataSource = new ArrayDataView(array);
或者您可以尝试:
var data = new double[5,5]
{
{ 0.000976563, 0.000976563, 0.000976563, 0.000976563, 0.000976563 },
{ 0.000976563, 0.000976563, 0.000976563, 0.000976563, 0.000976563 },
{ 0.000976563, 0.000976563, 0.000976563, 0.000976563, 0.000976563 },
{ 0.000976563, 0.000976563, 0.000976563, 0.000976563, 0.000976563 },
};
var rowCount = data.GetLength(0);
var rowLength = data.GetLength(1);
for (int rowIndex = 0; rowIndex < rowCount; ++rowIndex)
{
var row = new DataGridViewRow();
for(int columnIndex = 0; columnIndex < rowLength; ++columnIndex)
{
row.Cells.Add(new DataGridViewTextBoxCell()
{
Value = data[rowIndex, columnIndex]
});
}
dataGrid1.Rows.Add(row);
}