问题描述
您好,我在过去2天一直在解决该问题.我想从文本框中获取数据,然后单击按钮,我希望将该数据插入到gridview中.每次单击都会创建一个新行,并且绝不能删除旧行.这是我的代码.每当我输入新数据并单击按钮时,我的旧行就会被删除,而新行将代替它存储.请帮助我解决这个问题.
hi i have been stuck on that problem for last 2 days. i want to get the data from textboxes and on button click i want that data to be inserted in gridview. on every click there shud be a new row created and old row must not be removed. here is my code. whenever i enter the new data and click on button my old row is deleted and new row is stored in place of it. Kindly help me with my that problem thanx.
private void gridVIEWData()
{
dt1.Columns.Add("pName", typeof(string));
dt1.Columns.Add("pCategory", typeof(string));
dt1.Columns.Add("price", typeof(string));
dt1.Columns.Add("pQuantity", typeof(string));
dt1.Columns.Add("totalPrice", typeof(string));
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void TextBox4_TextChanged(object sender, EventArgs e)
{
}
protected void txt_productCode_TextChanged(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
if (!flag)
{
gridVIEWData();
flag = true;
Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
DataRow dr = dt1.NewRow();
dr["pName"] = DropDownList2.SelectedItem;
dr["pCategory"] = DropDownList1.SelectedItem;
dr["price"] = txt_price.Text;
dr["pQuantity"] = txt_quantity.Text;
dr["totalPrice"] = total;
dt1.Rows.Add(dr);
GridView1.DataSource = dt1;
GridView1.DataBind();
}
else if (!IsPostBack)
{
Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
DataRow dr = dt1.NewRow();
dr["pName"] = DropDownList2.SelectedItem;
dr["pCategory"] = DropDownList1.SelectedItem;
dr["price"] = txt_price.Text;
dr["pQuantity"] = txt_quantity.Text;
dr["totalPrice"] = total;
dt1.Rows.Add(dr);
GridView1.DataSource = dt1;
GridView1.DataBind();
}
//else if (!IsPostBack)
//{
// Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
// DataRow dr = dt1.NewRow();
// dr["pName"] = DropDownList2.SelectedItem;
// dr["pCategory"] = DropDownList1.SelectedItem;
// dr["price"] = txt_price.Text;
// dr["pQuantity"] = txt_quantity.Text;
// dr["totalPrice"] = total;
// dt1.Rows.Add(dr);
// GridView1.DataSource = dt1;
// GridView1.DataBind();
//}
}
}
推荐答案
DataTable dt1=new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["a"] = null;
}
}
protected void Button3_Click(object sender, EventArgs e)
{
if Session["a"]==null)
{
gridVIEWData();
//flag= true;
Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
DataRow dr = dt1.NewRow();
dr["pName"] = DropDownList2.SelectedItem;
dr["pCategory"] = DropDownList1.SelectedItem;
dr["price"] = txt_price.Text;
dr["pQuantity"] = txt_quantity.Text;
dr["totalPrice"] = total;
dt1.Rows.Add(dr);
Session["datas"]=dt1;
GridView1.DataSource = dt1;
GridView1.DataBind();
}
else {
Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
DataTable dtcached = (DataTable)Session["a"];
DataRow dr = dtcached.NewRow();
dr["pName"] = DropDownList2.SelectedItem;
dr["pCategory"] = DropDownList1.SelectedItem;
dr["price"] = txt_price.Text;
dr["pQuantity"] = txt_quantity.Text;
dr["totalPrice"] = total;
DataTable dtCached=(DataTable)Session["datas"];
dtCached.Rows.Add(dr);
GridView1.DataSource = dtCached; GridView1.DataBind();
}
}
更改了代码,我已经用粗体显示了它,希望对您有所帮助.使用大量会话变量并不是很好,因为它会给服务器增加更多的负载,但是会给您一个提示
Changed code I have shown it in BOLD, Hope this helps.. It is not good to use lot of session variable as it adds more loads to server, but it gives you a hint
这篇关于单击按钮时如何从“文本框"将数据插入GridView中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!