Datagrid视图中的参数超出范围异常

时间:2019-08-23 10:54:53

标签: c# datagridview

我需要操作从sql数据库添加到Datagridview的数据,但是我收到一个超出范围异常的参数“索引超出范围。必须为非负数并且小于集合的大小。”这是我的代码。 SQL数据只是1-10之间的整数

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source=HAIER-PC;Initial Catalog=pdcs;Integrated Security=True;");
    con.Open();
    SqlCommand cnd = new SqlCommand("SELECT * from CDes where Age='" + comboBox1.Text + "' AND Exper='" + comboBox2.Text + "'AND Design='" + comboBox3.Text + "'  ", con);
    SqlDataReader rdr = cnd.ExecuteReader();
    while (rdr.Read())

    {
        int n = dataGridView1.Rows.Add();
        dataGridView1.Rows[n].Cells[0].Value = rdr["C_ID"].ToString();
        dataGridView1.Rows[n].Cells[1].Value = rdr["Age"].ToString();
        dataGridView1.Rows[n].Cells[2].Value = rdr["Exper"].ToString();
        dataGridView1.Rows[n].Cells[3].Value = rdr["Design"].ToString();
        dataGridView1.Rows[n].Cells[4].Value = rdr["Shell"].ToString();
        dataGridView1.Rows[n].Cells[5].Value = rdr["Base"].ToString();
        dataGridView1.Rows[n].Cells[6].Value = rdr["Vnt"].ToString();
        dataGridView1.Rows[n].Cells[7].Value = rdr["Price"].ToString();
        dataGridView1.Rows[n].Cells[8].Value = rdr["Impactliner"].ToString();
        dataGridView1.Rows[n].Cells[9].Value = rdr["Eyeport"].ToString();
        dataGridView1.Rows[n].Cells[10].Value = rdr["Face"].ToString();
        dataGridView1.Rows[n].Cells[11].Value = rdr["Comfort"].ToString();
        dataGridView1.Rows[n].Cells[12].Value = rdr["Strap"].ToString();
        dataGridView1.Rows[n].Cells[13].Value = rdr["Wgt"].ToString();
    }
    rdr.Close();
    con.Close();

}

private void button2_Click(object sender, EventArgs e)
{
    double w1=2.67;
    for (int i = 0; i <= dataGridView1.Rows.Count; i++)
    {
        double shell = Convert.ToDouble(dataGridView1.Rows[i].Cells[5].Value); //here is the exception
        double s1 =  shell -  w1;
        textBox1.Text = s1.ToString();
    }
}

数据已经添加到datagridview

0 个答案:

没有答案