2016-06-07 40 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"]; 
相关问题