我是比較新的節目,所以很容易!數據網格迭代錯誤。退出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
是的,我可以確認它仍然不起作用:(該屬性不能用這種方法調用。謝謝你的幫助。 –