C# 数据网格迭代错误。为空时退出循环?

C# 数据网格迭代错误。为空时退出循环?,c#,loops,datagridview,iteration,indexoutofrangeexception,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. 如何防止在遍历datagridview后发生这些错误。这段

我对编程比较陌生,所以要简单

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.
如何防止在遍历datagridview后发生这些错误。这段代码正是我们所需要的。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);


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

非常感谢。

因此基本上您需要使用
row
变量,而不是
dataGridView1.Rows[i]
退出参数异常的原因是,自
dataGridView1.Rows[i]以来,行数小于收到的列数
line尝试访问不存在的数组的索引

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);
}
for(int-rowIndex=0;rowIndex
你可能还想看看Dean, 尝试引用DataGridView中不存在的行或列时,将导致“超出范围”错误。在您的情况下,计数器(i)重复计算列的数量,但代码引用了DGV的。修复该错误将解决您的问题

回应您的评论: 更换线路:

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

for(int i=0;i

应该可以防止错误发生,但是如果不确切知道你想要完成什么,就很难进一步指导你。这里有一个嵌套循环,从我看到的情况来看,有很多是多余的。

谢谢,我了解错误发生的原因。它的编码,使它不会发生我坚持。有什么想法吗?DeanI已经更新了我的答案来回答你的问题。嗨,Jim,你更新的评论对循环有相同的反应吗?我已取出冗余for循环。:)我希望for循环在遍历数据网格行时中断/停止,以防止错误发生。当所有3行都提取了数据时。停止谢谢你的帮助嗨,谢谢你的帮助。但由于row.Cells[]的原因,这不起作用。红色下划线表示“对象不包含单元格定义……”等。还有其他建议吗?@DeanCosgrove基于并且您的代码
DataGridViewRow
有一个名为
cells
的属性。您能否确认区分大小写是否正确。我还更新了代码,因为您不需要第二个循环,因为您已经硬编码了单元格编号。啊,我可以确认它仍然不起作用:(无法使用此方法调用该属性。感谢帮助人员。@DeanCosgrove别担心,我还添加了另一种方法,用于以索引的形式访问行,它更类似于您自己的原始代码。如果不查看您的原始代码,将无法提供更多帮助。还需要知道确切的错误消息,以便能够帮助修复它。)g、 非常感谢!!救生员。我的论文会感谢你的…哈哈。干杯,伙计,谢谢。
for (int i = 0; i < dataGridView1.Rows.Count; i++)