0

GridView虽然在抛出的错误 上设置数据是Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

try
{
    con.Open();
    SqlCommand cmd = new SqlCommand("gridalldata", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@order_no", SqlDbType.NVarChar).Value = txt_orderno.Text;
    cmd.ExecuteNonQuery();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dtr =new DataTable();
    da.Fill(dtr);
    MessageBox.Show(""+dtr.Rows[0][0].ToString());
    for (search = 0; search < dtr.Rows.Count-1; search++)
    {

        dataGridView1.Rows[search].Cells[0].Value = dtr.Rows[search][0].ToString();
        dataGridView1.Rows[search].Cells[1].Value = dtr.Rows[search][1].ToString();
        dataGridView1.Rows[search].Cells[2].Value = dtr.Rows[search][2].ToString();
        dataGridView1.Rows[search].Cells[3].Value = dtr.Rows[search][3].ToString();
        dataGridView1.Rows[search].Cells[4].Value = dtr.Rows[search][4].ToString();
        dataGridView1.Rows[search].Cells[5].Value = dtr.Rows[search][5].ToString();
        dataGridView1.Rows[search].Cells[6].Value = dtr.Rows[search][6].ToString();
        dataGridView1.Rows[search].Cells[7].Value = dtr.Rows[search][7].ToString();
        dataGridView1.Rows[search].Cells[8].Value = dtr.Rows[search][8].ToString();
        txt_discount.Text = dtr.Rows[search][10].ToString();
        txt_netamount.Text = dtr.Rows[search][11].ToString();
        dataGridView1.Rows[search].Cells[10].Value = dtr.Rows[search][12].ToString();
        dataGridView1.Rows[search].Cells[9].Value = dtr.Rows[search][13].ToString();

    }

 }

 catch (Exception ex)
 {
     MessageBox.Show(ex.Message);
 }
 finally { con.Close(); }

谁能指导我?

4

1 回答 1

0

尝试这个:

dataGridView1.Rows[search].Cells[10].Value = dtr.Rows[search][10].ToString();
dataGridView1.Rows[search].Cells[9].Value = dtr.Rows[search][9].ToString();
于 2013-05-28T09:23:28.147 回答