数据网格迭代错误。当为null时退出For循环?

时间:2017-05-06 22:52:08

标签: c# loops datagridview iteration indexoutofrangeexception

我对编程比较陌生,所以很容易!

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

如何在迭代数据网格视图后防止这些错误发生。代码完全满足需要。 for循环遍历所有行但在没有行时抛出异常。如何在转换所有行时突破?

foreach (DataGridViewRow row in dataGridView1.Rows)
        {


            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                //String header = gridview_id.Columns[i].HeaderText;
               // String cellText = row.Cells[i].Text;

                partData.partID = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
                partData.productName = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
                partData.partDescription = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value);
                partData.unitPrice = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value);
                partData.quantity = Convert.ToString(dataGridView1.Rows[i].Cells[4].Value);
                partData.partNote = Convert.ToString(dataGridView1.Rows[i].Cells[5].Value);

                MessageBox.Show(PerformRequestUpdatePriority("http://dmcalla04.students.qub.ac.uk/import.php?", username, partData.partID, partData.productName, partData.partDescription, partData.unitPrice, partData.quantity, partData.partNote));
                Console.WriteLine(username);


            }
        }

非常感谢提前。

2 个答案:

答案 0 :(得分:0)

所以基本上你需要使用row变量而不是dataGridView1.Rows[i]你出现参数异常的原因是在行数小于数量的情况下自dataGridView1.Rows[i]行尝试访问不存在的数组索引以来收到的列。

foreach (var row in dataGridView1.Rows)
{
    partData.partID = Convert.ToString(row.Cells[0].Value);
    partData.productName = Convert.ToString(row.Cells[1].Value);
    partData.partDescription = Convert.ToString(row.Cells[2].Value);
    partData.unitPrice = Convert.ToString(row.Cells[3].Value);
    partData.quantity = Convert.ToString(row.Cells[4].Value);
    partData.partNote = Convert.ToString(row.Cells[5].Value);

    MessageBox.Show(PerformRequestUpdatePriority("http://dcosgrove04.students.cs.qub.ac.uk/importParts.php?", username, partData.partID, partData.productName, partData.partDescription, partData.unitPrice, partData.quantity, partData.partNote));
    Console.WriteLine(username);
}

或者如果您想使用索引,可以使用以下代码:

for (int rowIndex = 0; rowIndex < dataGrid.Rows.Count; rowIndex++)
{
    var row = dataGridView1.Rows[rowIndex];
    partData.partID = Convert.ToString(row.Cells[0].Value);
    partData.productName = Convert.ToString(row.Cells[1].Value);
    partData.partDescription = Convert.ToString(row.Cells[2].Value);
    partData.unitPrice = Convert.ToString(row.Cells[3].Value);
    partData.quantity = Convert.ToString(row.Cells[4].Value);
    partData.partNote = Convert.ToString(row.Cells[5].Value);

    MessageBox.Show(PerformRequestUpdatePriority("http://dcosgrove04.students.cs.qub.ac.uk/importParts.php?", username, partData.partID, partData.productName, partData.partDescription, partData.unitPrice, partData.quantity, partData.partNote));
    Console.WriteLine(username);
}

您可能还想查看this answer

答案 1 :(得分:0)

迪安, 当您尝试引用不存在的DataGridView中的行或列时,超出范围&#39;会导致错误。在您的情况下,您的计数器(i)会重复 的数量,但您的代码会引用DGV的 行< / EM> 即可。修复该错误应解决您的问题。

回复您的评论: 替换线:

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

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

应该可以防止错误,但如果不确切地知道您要完成的是什么,则很难为您提供更多指导。你在那里有一个嵌套的循环,从我所看到的,那里有很多是多余的。