Tôi muốn lấy dữ liệu từ hộp văn bản và bấm vào nút, tôi muốn dữ liệu đó được chèn vào GridView
. Trên mỗi nhấp chuột, sẽ có một hàng mới được tạo và không được xóa hàng cũ. Bất cứ khi nào tôi nhập dữ liệu mới và bấm vào nút, hàng cũ của tôi bị xóa và hàng mới được lưu trữ thay cho nó. Đây là mã của tôi:Làm thế nào để chèn dữ liệu từ hộp văn bản vào GridView trên nút bấm trong ASP.NET
DataTable dt1 = new DataTable();
bool flag = false;
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 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();
}
}
Điều gì xảy ra trong 'Page_Load'? Cân nhắc đổi tên các nút của bạn. Mục đích của 'Button3' không rõ ràng. –