【问题标题】:excpetion error :Index was out of range. Must be non-negative and less than the size of the collection异常错误:索引超出范围。必须是非负数且小于集合的大小
【发布时间】: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"] };
}

【问题讨论】:

标签: c# sql datagridview


【解决方案1】:

问题是indexer on the DataGridView
第一个索引是 ColumnIndex,第二个是 RowIndex

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

【讨论】:

    猜你喜欢
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    相关资源
    最近更新 更多