繁体   English   中英

excpetion error:索引超出范围。 必须是非负数且小于集合的大小

[英]excpetion error :Index was out of range. Must be non-negative and less than the size of the collection

我已经看到了所有异常错误,但我没有遇到问题。 我有tblsecondary有streetId,streetName,fasileOne,fasileTwo。 当我在datagridviewcomboboxcolumn中选择一个值时,我正在使用datagridview来填充行,但是它给了我一个异常错误:

指数超出范围。 必须是非负数且小于集合的大小。

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"] };
}

问题是DataGridView上的索引器
第一个索引是ColumnIndex,第二个是RowIndex

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM