2017-05-06 75 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行都有提取的數據。停止。謝謝您的幫助 –