2016-06-07 70 views
1

我已經看到所有異常錯誤,但我沒有得到我的問題。我有tblsecondary有streetId,streetName,fasileOne,fasileTwo。我在datagridviewcomboboxcolumn中選擇一個值時使用datagridview來填充行,但它給了我一個異常錯誤:excpetion錯誤:索引超出範圍。必須是非負值且小於集合的大小

索引超出範圍。必須是非負數且小於集合的大小。

on dataGridView7 [e.RowIndex,2] .Value = drFound [「fasileOne」];

的代碼是:

private void dataGridView7_CellValueChanged(object sender, DataGridViewCellEventArgs e) 
     { 
      if ((e.RowIndex >= 0) && (e.ColumnIndex == 1)) // Assuming this is the streetId 
      { 
       int streetId = Convert.ToInt16(dataGridView7.Rows[e.RowIndex].Cells[e.ColumnIndex].Value); 
       DataRow drFound = tblSecondary.Rows.Find(streetId); 
       if (drFound == null) 
       { 
        MessageBox.Show("ddhhdhd"); 
       } 
       else 
       { 
        dataGridView7[e.RowIndex, 2].Value = drFound["fasileOne"]; 
        dataGridView7[e.RowIndex, 3].Value = drFound["fasileTwo"]; 
       } 
      } 

下面是數據表正在使用:

public IncidentForm() 
tblSecondary = new DataTable(); 
SqlCommand cmd2 = cnn.CreateCommand(); 
cmd2.CommandText = "select * from street"; 
SqlDataAdapter sdr2 = new SqlDataAdapter(cmd2); 
cmd2.CommandType = CommandType.Text; 
cmd2.Connection = cnn; 
sdr2.Fill(tblSecondary); 
tblSecondary.PrimaryKey = new DataColumn[] { tblSecondary.Columns["streetId"] }; 
} 
+0

它列則行,再不行行列。 https://msdn.microsoft.com/en-us/library/ms158656(v=vs.110).aspx – juharr

+0

是的,它的工作非常感謝你,先生。 –

+0

我應該使用什麼事件,當我改變第一個組合框值時,所有被刪除,直到我選擇第二個組合框,以便該行更新 –

回答

0

的問題是indexer on the DataGridView
第一個索引器的columnIndex,第二個的rowIndex

dataGridView7[2, e.RowIndex].Value = drFound["fasileOne"]; 
dataGridView7[3, e.RowIndex].Value = drFound["fasileTwo"]; 
相關問題