我尝试在我的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;
}
}
答案 0 :(得分:2)
通过将创建的数据表添加到会话状态来修改代码,然后在用户单击按钮时尝试检索它。
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;
}
}
答案 1 :(得分:1)
由于您没有在数据库中保存数据,因此您需要创建一个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;
答案 2 :(得分:0)
为我工作的答案就是这个
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;
}
}