[英]excpetion error :Index was out of range. Must be non-negative and less than the size of the collection
I have seen all the exception errors but I didn't get my problem. 我已经看到了所有异常错误,但我没有遇到问题。 I have tblsecondary which have streetId,streetName,fasileOne,fasileTwo. 我有tblsecondary有streetId,streetName,fasileOne,fasileTwo。 I am using a datagridview to populate a row when I select a value in datagridviewcomboboxcolumn, but its giving me an exception error : 当我在datagridviewcomboboxcolumn中选择一个值时,我正在使用datagridview来填充行,但是它给了我一个异常错误:
Index was out of range. 指数超出范围。 Must be non-negative and less than the size of the collection. 必须是非负数且小于集合的大小。
on dataGridView7[e.RowIndex, 2].Value = drFound["fasileOne"]; on dataGridView7 [e.RowIndex,2] .Value = drFound [“fasileOne”];
The code is : 代码是:
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"];
}
}
Here is the datatable am using : 这是我使用的数据表:
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"] };
}
The problem is the indexer on the DataGridView . 问题是DataGridView上的索引器 。
The first index is the ColumnIndex, the second the RowIndex 第一个索引是ColumnIndex,第二个是RowIndex
dataGridView7[2, e.RowIndex].Value = drFound["fasileOne"];
dataGridView7[3, e.RowIndex].Value = drFound["fasileTwo"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.