我是比较新的节目,所以很容易!数据网格迭代错误。退出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);
}
}
很多在此先感谢。
嗨,谢谢你的帮助。但由于row.Cells [],这不起作用。应当强调指出红“对象不包含电池的认定中......”等任何其他建议? –
@DeanCosgrove基于[此链接](https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.cells(V = vs.110)的.aspx),还可以将代码' DataGridViewRow'有一个名为'Cells'的属性。你能否确认区分大小写是否正确。我也更新了代码,因为你不需要第二个循环,因为你已经硬编码了单元格号 – manman
是的,我可以确认它仍然不起作用:(该属性不能用这种方法调用。谢谢你的帮助。 –