簡體   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