2017-05-06 70 views
0

我是比较新的节目,所以很容易!数据网格迭代错误。退出For循环时null?

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); 


      } 
     } 

很多在此先感谢。

回答

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

+0

嗨,谢谢你的帮助。但由于row.Cells [],这不起作用。应当强调指出红“对象不包含电池的认定中......”等任何其他建议? –

+0

@DeanCosgrove基于[此链接](https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.cells(V = vs.110)的.aspx),还可以将代码' DataGridViewRow'有一个名为'Cells'的属性。你能否确认区分大小写是否正确。我也更新了代码,因为你不需要第二个循环,因为你已经硬编码了单元格号 – manman

+0

是的,我可以确认它仍然不起作用:(该属性不能用这种方法调用。谢谢你的帮助。 –

0

院长, 当您尝试访问的行或列中DataGridView不存在,将导致“超出范围”错误。在你的情况,你的计数器(I)将重复的数量,但您的代码参照DGV的。修复该错误应该可以解决您的问题。

针对您的评论: 更换行:

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

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

应防止错误,但不知道到底你想实现什么,这是很难进一步引导你。你已经有了一个嵌套循环在那里,从我所看到的,有很多在那里,是多余的。

+0

谢谢,我明白了什么在关于错误发生的事情,它的编码它,使其不发生,我坚持我与。任何想法?院长 –

+0

我已经更新了我的回答以回应你的问题。 –

+0

嗨吉姆,你的更新评论得到了相同的循环?我已经取出了循环冗余。 :)我希望for循环在数据网格行迭代时阻止/停止,以防止发生错误。例如。当所有3行都有提取的数据。停止。谢谢您的帮助 –