【发布时间】:2016-06-07 14:14:56
【问题描述】:
我已经看到了所有的异常错误,但我没有遇到我的问题。我有 tblsecondary,其中有 streetId、streetName、fasileOne、fasileTwo。当我在 datagridviewcomboboxcolumn 中选择一个值时,我正在使用 datagridview 填充一行,但它给了我一个异常错误:
索引超出范围。必须为非负数且小于集合的大小。
关于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"] };
}
【问题讨论】:
-
先列后行,而不是先行后列。 msdn.microsoft.com/en-us/library/ms158656(v=vs.110).aspx
-
是的,它成功了,非常感谢你先生。
-
当我改变第一个组合框值时我应该使用什么事件,直到我选择第二个组合框以便更新行
标签: c# sql datagridview