[英]How to add a new row in DataTable below the existing rows
我試圖在我的DataTable
添加一個新行,然后每次單擊按鈕將其綁定到GridView
( Inquiries
)。 當我第一次單擊該按鈕時添加該行。 但是當我嘗試再次點擊它來添加另一行時,它會替換現有的行。
protected void addInquiry_Click(object sender, EventArgs e)
{
try
{
Quantity = QuantityTxt.Text;
string Details = Request.Form[Products.UniqueID];
SelectedProduct = Details.Split('!');
ProductNo = SelectedProduct[0];
ProductDescription = SelectedProduct[1];
ProductSapPack = SelectedProduct[2];
ProductID = SelectedProduct[3];
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[5] {
new DataColumn("ProductID",typeof(int)),
new DataColumn("ProductNo", typeof(string)),
new DataColumn("Description", typeof(string)),
new DataColumn("SapPack",typeof(string)),
new DataColumn("Quantity",typeof(string)),
});
DataRow dr = dt.NewRow();
dr[0] = ProductID;
dr[1] = ProductNo;
dr[2] = ProductDescription;
dr[3] = ProductSapPack;
dr[4] = Quantity;
dt.Rows.Add(dr);
Inquiries.DataSource = dt;
Inquiries.DataBind();
}
finally
{
QuantityTxt.Text = String.Empty;
Description.Text = String.Empty;
SapPack.Text = String.Empty;
Products.Text = String.Empty;
}
}
通過將創建的數據表添加到會話狀態來修改代碼,然后在用戶單擊按鈕時嘗試檢索它。
protected void addInquiry_Click(object sender, EventArgs e)
{
DataTable dt=(DataTable)Session["Inquiry"];
try
{
Quantity = QuantityTxt.Text;
string Details = Request.Form[Products.UniqueID];
SelectedProduct = Details.Split('!');
ProductNo = SelectedProduct[0];
ProductDescription = SelectedProduct[1];
ProductSapPack = SelectedProduct[2];
ProductID = SelectedProduct[3];
if(dt==null)
{
dt= new DataTable();
}
dt.Columns.AddRange(new DataColumn[5] {
new DataColumn("ProductID",typeof(int)),
new DataColumn("ProductNo", typeof(string)),
new DataColumn("Description", typeof(string)),
new DataColumn("SapPack",typeof(string)),
new DataColumn("Quantity",typeof(string)),
});
DataRow dr = dt.NewRow();
dr[0] = ProductID;
dr[1] = ProductNo;
dr[2] = ProductDescription;
dr[3] = ProductSapPack;
dr[4] = Quantity;
dt.Rows.Add(dr);
Inquiries.DataSource = dt;
Inquiries.DataBind();
Session.Add("Inquiry",dt);
}
finally
{
QuantityTxt.Text = String.Empty;
Description.Text = String.Empty;
SapPack.Text = String.Empty;
Products.Text = String.Empty;
}
}
由於您沒有在數據庫中保存數據,因此需要創建Session / ViewState來保存DataTable。
if(Session["Products"] != null)
dt = (DataTable) Session["Products"];
else
dt = new DataTable(); // For the first time creates a new DataTable otherwise reads from Session variable
----
At the end, save the data to the Session variable:
Session["Products"] = dt;
為我工作的答案是這樣的
protected void addInquiry_Click(object sender, EventArgs e)
{
DataTable dt=(DataTable)Session["Inquiry"];
try
{
Quantity = QuantityTxt.Text;
string Details = Request.Form[Products.UniqueID];
SelectedProduct = Details.Split('!');
ProductNo = SelectedProduct[0];
ProductDescription = SelectedProduct[1];
ProductSapPack = SelectedProduct[2];
ProductID = SelectedProduct[3];
if(dt==null)
{
dt= new DataTable();
dt.Columns.AddRange(new DataColumn[5] {
new DataColumn("ProductID",typeof(int)),
new DataColumn("ProductNo", typeof(string)),
new DataColumn("Description", typeof(string)),
new DataColumn("SapPack",typeof(string)),
new DataColumn("Quantity",typeof(string)),
});
}
DataRow dr = dt.NewRow();
dr[0] = ProductID;
dr[1] = ProductNo;
dr[2] = ProductDescription;
dr[3] = ProductSapPack;
dr[4] = Quantity;
dt.Rows.Add(dr);
Inquiries.DataSource = dt;
Inquiries.DataBind();
Session.Add("Inquiry",dt);
}
finally
{
QuantityTxt.Text = String.Empty;
Description.Text = String.Empty;
SapPack.Text = String.Empty;
Products.Text = String.Empty;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.