2017-08-06 132 views
0

我有一個有dataGridView並將其值傳遞給文本框的表單,但當我單擊任何列時,我總是收到ArgumentOutOfRangeExceptionDataGridView越來越ArgumentOutOfRangeException

如果只有行被點擊,這些代碼工作正常。

private void dataGridProd_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    { 
     DataGridViewRow row = this.dataGridProd.Rows[e.RowIndex]; 

     foreach (DataGridViewColumn column in dataGridProd.Columns) 
     { 
      column.SortMode = DataGridViewColumnSortMode.NotSortable; 

      txBName.Text = row.Cells[1].Value.ToString(); 
      txBPrice.Text = row.Cells[2].Value.ToString(); 
     } 
     btnAdd.Enabled = true; 
     prodQuanUpDown.Enabled = true; 
    } 

任何類型的響應非常感謝。先謝謝你!

+0

異常發生在哪一行? – Ben

+0

關於productName和productPrice – vicserna1997

+0

row.Cells數組中有多少項? – KiwiPiet

回答

0

首先看不到爲什麼你需要foreach循環這裏和每個帖子,如果你得到ArgumentOutOfRangeException那麼它必須是因爲下面兩行。你真的有那麼多行的列嗎?您可以在Cells集說row.Cells.Count

txBName.Text = row.Cells[1].Value.ToString(); 
txBPrice.Text = row.Cells[2].Value.ToString(); 
0

是產品的產品名稱檢查Count屬性確實在該表的第2列,如果它是在第一欄應該是row.Cells[0].Value.toString();

row.Cells[1].Value.toString(); < - - 這是第二列thats你的價格,那麼如果它是在第二列

希望此答案有幫助!