[英]Populating multiple datagridviews from a single 2 dimensional array
I recently posted a question on how to populate a 2 dimensional array with data from 7 datagridviews, each with 7 rows x 5 columns. 我最近发布了一个问题,关于如何用来自7个datagridviews的数据填充二维数组,每个datagridviews具有7行x 5列。 Enigmativity supplied me with a piece of code that worked great, many thanks.
困惑为我提供了一段非常有效的代码,非常感谢。
public string[,] myGridData = new string[50, 5];
//49 data rows, last row (50) contains version number
var dgvs = new[] { dgv6, dgv5, dgv4, dgv3, dgv2, dgv1, dgv0, };
for (var i = 0; i < dgvs.Length; i++)
{
for (int rows = 0; rows < 7; rows++)
{
for (int col = 0; col < 5; col++)
{
myGridData[rows + i * 7, col] = dgvs[i].Rows[rows].Cells[col].Value.ToString();
}
}
}
I now need a way to reverse the procedure and populate the 7 datagridviews from the original array. 我现在需要一种方法来颠倒该过程,并从原始数组中填充7个datagridviews。
I know how to populate a single datagridview from a 2d array, but not sure how to populate all 7 grids. 我知道如何从2d数组填充单个datagridview,但不确定如何填充所有7个网格。 The code I have been using to fill 1 grid is:
我用来填充1个网格的代码是:
//Populate Single DataViewGrid
var rowCount = myGridData.GetLength(0);
var rowLength = myGridData.GetLength(1);
dgv1.ColumnCount = 5;
for (int rowIndex = 0; rowIndex < rowCount - 1; ++rowIndex)
{
var row = new DataGridViewRow();
for (int columnIndex = 0; columnIndex < rowLength; ++columnIndex)
{
row.Cells.Add(new DataGridViewTextBoxCell()
{
Value = myGridData[rowIndex, columnIndex]
});
}
dgv1.Rows.Add(row);
}
Thanks in advance. 提前致谢。
Just like populating the 2d array, you need to iterate through the 7 DataGridView
controls. 就像填充2d数组一样,您需要遍历7个
DataGridView
控件。 Nesting your code within that loop, you then change how far your inner loop iterates. 将代码嵌套在该循环中,然后更改内部循环的迭代距离。 Instead of iterating
rowCount - 1
, filling one DataGridView
with all 49 rows, you only iterate over the next 7 rows. 无需迭代
rowCount - 1
(使用全部49行填充一个DataGridView
,而是仅迭代接下来的7行。 By moving rowIndex
outside of the outermost loop, it will "remember" the next index as you progress through each view. 通过将
rowIndex
移动到最外层循环之外,它将在您浏览每个视图时“记住”下一个索引。
var rowCount = myGridData.GetLength(0);
var rowLength = myGridData.GetLength(1);
int rowIndex = 0;
var dgvs = new[] { dgv6, dgv5, dgv4, dgv3, dgv2, dgv1, dgv0, };
for (int i = 0; i < dgvs.Length; i++)
{
var dgv = dgvs[i];
dgv.ColumnCount = 5;
for (int taken = 0; taken < 7; ++taken, ++rowIndex)
{
var row = new DataGridViewRow();
for (int columnIndex = 0; columnIndex < rowLength; ++columnIndex)
{
row.Cells.Add(new DataGridViewTextBoxCell()
{
Value = myGridData[rowIndex, columnIndex]
});
}
dgv.Rows.Add(row);
}
}
If it makes you feel better, you can modify the inner loop to: 如果感觉更好,可以将内部循环修改为:
for (int taken = 0; taken < 7 && rowIndex < rowCount - 1; ++taken, ++rowIndex)
But honestly it will still execute the same and is creating an additional logic check for each iteration. 但老实说,它仍将执行相同的操作,并为每次迭代创建一个附加的逻辑检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.