Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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上一个索引超出范围_C#_Datagridview_Cells - Fatal编程技术网

C#Datagridview上一个索引超出范围

C#Datagridview上一个索引超出范围,c#,datagridview,cells,C#,Datagridview,Cells,嗨,伙计们,我是新来的=) 在一个最小的麻烦案件中,我需要你的帮助,为我节省时间=) 问题在于,我想要datagridview单元格的值和 将值保存到数组中 到目前为止,它工作正常,但它只读取第一行单元格和之后的单元格 这个过程只是告诉我们索引超出了范围,但我不明白为什么 foreach (DataGridViewRow row in dataGridView1.Rows) //wo soll er sich aufhalten -> zeilen

嗨,伙计们,我是新来的=)

在一个最小的麻烦案件中,我需要你的帮助,为我节省时间=)

问题在于,我想要datagridview单元格的值和 将值保存到数组中

到目前为止,它工作正常,但它只读取第一行单元格和之后的单元格 这个过程只是告诉我们索引超出了范围,但我不明白为什么

foreach (DataGridViewRow row in dataGridView1.Rows)     //wo soll er sich aufhalten -> zeilen
                {

                    **if (row.Cells[i].Value == null)**
                    {
                        MessageBox.Show("This row is empty");
                        break;
                    }
                        if (row.Cells[i].Value != null)
                        {
                            UnsortArray[i] = row.Cells[i].Value.ToString();
                            MessageBox.Show(UnsortArray[i]);
                            ++i;
                        }

                }
foreach(dataGridView1.Rows中的DataGridViewRow行)
{
for(int i=0;i
foreach(dataGridView1.Rows中的DataGridViewRow行)
{

对于(inti=0;i请尝试以下代码>>

for(int i=0;i<dataGridView1.Rows.Count;i++)
                {

                    if (dataGridView1.Rows[i].cells[i].Value==null)**
                    {
                        MessageBox.Show("This row is empty");
                        break;
                    }

                    else
                        {
                          UnsortArray[i] = dataGridView1.Rows[i].cells[i].Value.ToString();
                            MessageBox.Show(UnsortArray[i]);

                        }





                }

for(inti=0;i试试下面的代码>>

for(int i=0;i<dataGridView1.Rows.Count;i++)
                {

                    if (dataGridView1.Rows[i].cells[i].Value==null)**
                    {
                        MessageBox.Show("This row is empty");
                        break;
                    }

                    else
                        {
                          UnsortArray[i] = dataGridView1.Rows[i].cells[i].Value.ToString();
                            MessageBox.Show(UnsortArray[i]);

                        }





                }

for(int i=0;i对
unsortary
(比如j)使用不同的索引变量。
i
是要在
unsortary
中存储值的单元格索引

代码

            foreach (DataGridViewRow row in dataGridView1.Rows)     
            {

                if (row.Cells[i].Value == null)
                {
                    MessageBox.Show("This row is empty")
                }
                else
                    {
                        UnsortArray[j] = row.Cells[i].Value.ToString();
                        MessageBox.Show(UnsortArray[j]);
                        ++j;
                    }
            }

unsortary
(比如j)使用不同的索引变量。
i
是要存储在
unsortary

代码

            foreach (DataGridViewRow row in dataGridView1.Rows)     
            {

                if (row.Cells[i].Value == null)
                {
                    MessageBox.Show("This row is empty")
                }
                else
                    {
                        UnsortArray[j] = row.Cells[i].Value.ToString();
                        MessageBox.Show(UnsortArray[j]);
                        ++j;
                    }
            }

什么是“i”。您使用foreach循环遍历所有行。您还需要遍历列。因为它尝试读取
单元格[length]
在下一行,这是超出范围的。使用两个变量作为索引,每行一个变量重置为0。因为我不断变大?你想在数组中只存储一个单元格值还是所有单元格值?在你的代码中,你混合了索引“i”。“i”用作单元格索引以及非排序数组索引。“i”是什么。您正在使用foreach循环遍历所有行。您还需要遍历列。因为它尝试读取
单元格[length]
在下一行,这是超出范围的。使用两个变量作为索引,每行一个变量重置为0。因为我不断变大?你想在数组中只存储一个单元格值还是所有单元格值?在你的代码中,你混合了索引“i”。“i”用作单元格索引以及非排序数组索引。非常感谢,你解决了我在快速可能方式=)我试图投票,但还不可能!以后再做!@Niko:我现在会代表你做的非常感谢你,你用快速可能方式解决了我的问题=)我试图投票,但还不可能!以后再做!@Niko:我现在会代表你做为什么你在if(row.Cells[I]中增加I值.Value!=null)块?对于已经增加i值的循环。为什么要在if(row.Cells[i].Value!=null)块中增加i值?对于已经增加i值的循环。