Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在DataGridView.Rows[]中使用For循环Int_C#_.net_For Loop - Fatal编程技术网

C# 在DataGridView.Rows[]中使用For循环Int

C# 在DataGridView.Rows[]中使用For循环Int,c#,.net,for-loop,C#,.net,For Loop,在我的WFA项目(C#)中,我需要将所有DataGridView值读取到数组中。我想使用for循环。 例如,对于第一行,我尝试使用i=0值,并尝试将值读取到数组。 喜欢 string[]Array1=新字符串[2]; 对于(int i=0;i

在我的WFA项目(C#)中,我需要将所有DataGridView值读取到数组中。我想使用for循环。 例如,对于第一行,我尝试使用i=0值,并尝试将值读取到数组。 喜欢

string[]Array1=新字符串[2];
对于(int i=0;i

我得到了错误,“我”没有值。我试着在印刷动作中使用它。我在BeginPrint事件中也给出了值。

我认为您需要类似这样的东西:

List<string[]> allValues = new List<string[]>();
foreach(var row in dvg1.Rows)
{
    string[] arr = new string[3];  
    arr[0] = row.Cells["Total1"].Value.ToString();
    arr[1] = row.Cells["Total2"].Value.ToString();
    arr[2] = row.Cells["Total3"].Value.ToString();

    allValues.add(arr);
}
List allValues=new List();
foreach(dvg1.Rows中的变量行)
{
字符串[]arr=新字符串[3];
arr[0]=row.Cells[“Total1”].Value.ToString();
arr[1]=row.Cells[“Total2”].Value.ToString();
arr[2]=row.Cells[“Total3”].Value.ToString();
所有值。添加(arr);
}

我不知道为什么我被否决了。除了编译器错误之外,我还试图解决循环中每次迭代都会覆盖自己的问题。

您的代码是正确的,但似乎存在一些错误,例如您没有为数组使用正确的口袋大小,第二,在为数组赋值时遗漏了数组名称

下面是帮助您的示例

string[] Array1 = new string[3];   

                for (int i = 0; i < dataGridView1.Rows.Count; i +=1)
                {
                    Array1 [0] = dataGridView1.Rows[i].Cells[0].Value.ToString();
                    Array1 [1] = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    Array1 [2] = dataGridView1.Rows[i].Cells[2].Value.ToString();
                }
string[]Array1=新字符串[3];
对于(int i=0;i
“我收到错误”又是什么错误?索引超出范围。必须为非负且小于集合的大小。该错误是否足够清楚?请检查numorrows是否可能大于datagridSure中的实际行数,这就足够了。但当我在行间使用MessageBox时,我可以看到i值,也可以读取真实值。但是我不能将thme用作行索引。@user3054228我是否有值并不重要,例如,如果i=10,而您只有5行,它将抛出您得到的异常,因此请注意,在这种情况下,请记住索引行从0开始,因此第一行是dgv.Rows[0],最大值是dgv.Rows[dgv.Rows.Count-1],谢谢您的回复。我要试试这个。
string[] Array1 = new string[3];   

                for (int i = 0; i < dataGridView1.Rows.Count; i +=1)
                {
                    Array1 [0] = dataGridView1.Rows[i].Cells[0].Value.ToString();
                    Array1 [1] = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    Array1 [2] = dataGridView1.Rows[i].Cells[2].Value.ToString();
                }