簡體   English   中英

如何在gridview文本框和組合框列上設置數據

[英]How to set data on gridview textboxes and combo boxes columns

GridView上設置數據時,拋出的錯誤是Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 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(); }

誰能指導我?

嘗試這個:

dataGridView1.Rows[search].Cells[10].Value = dtr.Rows[search][10].ToString();
dataGridView1.Rows[search].Cells[9].Value = dtr.Rows[search][9].ToString();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM