1
我正在從一個表中獲取數據到一個gridview。我的gridview有3個文本框和一個下拉控件。我想從數據庫中逐行讀取數據。獲取每行後,我想增加一個網格視圖的行並再次獲取它。如何增加gridview的行?
這裏是我的代碼
SqlCommand sqlCommand2 = new SqlCommand("proc_bcdet", sqlConnection);
sqlCommand2.CommandType = CommandType.StoredProcedure;
sqlConnection.Open();
sqlCommand2.Parameters.Add("@btcode", SqlDbType.Int).Value = Session["btcode"];
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = sqlCommand2;
DataSet ds = new DataSet();
adp.Fill(ds);
DataTable dt = ds.Tables[0];
int rowIndex = 0;
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)gvCashReceipt.Rows[i].Cells[1].FindControl("txtlgroup");
TextBox box2 = (TextBox)gvCashReceipt.Rows[i].Cells[2].FindControl("txtamt");
TextBox box3 = (TextBox)gvCashReceipt.Rows[i].Cells[3].FindControl("txtnarr");
Label lb1 = (Label)gvCashReceipt.Rows[i].Cells[4].FindControl("lbllcode");
DropDownList ddl1 = (DropDownList)gvCashReceipt.Rows[rowIndex].Cells[0].FindControl("drprledger");
//Fill the DropDownList with Data
ddl1.DataSource = module.Query("Select Name from AccMast where LGcode in (2,3,4) order by Lcode");
ddl1.DataBind();
if (i < dt.Rows.Count)
{
//Assign the value from DataTable to the TextBox
box1.Text = dt.Rows[i]["Lgname"].ToString();
box2.Text = dt.Rows[i]["Amt"].ToString();
box3.Text = dt.Rows[i]["Narr"].ToString();
lb1.Text = dt.Rows[i]["Lcode"].ToString();
//Set the Previous Selected Items on Each DropDownList on Postbacks
ddl1.ClearSelection();
ddl1.Items.FindByText(dt.Rows[i]["Name"].ToString()).Selected = true;
}
rowIndex++;
}
}
獲取第一行我得到一個錯誤「索引超出範圍」請幫我...之後
「我想從數據庫中逐行讀取數據」這通常不是一個好主意。在一次通話中獲取所需的全部數據。否則,您可以每頁加載數百個查詢......並且如果有數百人訪問同一頁面,結果就不盡如人意。 – MikeSmithDev