简体   繁体   English

C# - 从多个数据网格视图创建二维数组

[英]C# - Creating 2 dimensional array from multiple datagridviews

I am relatively new to c# and am having a problem filling a 2 dimensional array.我对 c# 比较陌生,并且在填充二维数组时遇到问题。

public string[,] myGridData = new string[50, 5];

The array data is contained in 7 datagridviews, each with 7rows x 5 columns.数组数据包含在 7 个数据网格视图中,每个视图有 7 行 x 5 列。 I know how to get the data from the first grid, but am not sure how to loop through all 7 grids.我知道如何从第一个网格中获取数据,但不确定如何遍历所有 7 个网格。

//Populate Array with data from grid 1  (7 rows,5 columns)
for (int rows = 0; rows < dgv1.Rows.Count; rows++)
{
    for (int col = 0; col < dgv1.Rows[rows].Cells.Count; col++)
    {
        myGridData[rows, col] = dgv1.Rows[rows].Cells[col].Value.ToString();
    }
}

Any help would be appreciated.任何帮助,将不胜感激。 Thank You谢谢你

Since you have said you have 7 data grid views and that each has dimensions 7 x 5 I have removed some of the loop constraints and replaced them with constants.由于您说过您有 7 个数据网格视图,并且每个视图的尺寸为 7 x 5,因此我删除了一些循环约束并将它们替换为常量。 I wouldn't normally recommend that, but since you are using 2 dimensional arrays and you've specified the values it is the simplest way to go.我通常不建议这样做,但由于您使用的是二维数组并且您已经指定了值,因此这是最简单的方法。

Here's the code:这是代码:

var dgvs = new [] { dgv1, dgv2, dgv3, dgv4, dgv5, dgv6, dgv7, };

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] = dgv1[i].Rows[rows].Cells[col].Value.ToString();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM