更新时间:2023-12-03 13:15:52
您应该拉你的数据转换成一个DataTable和/或数据集并将其存储在一个Session对象。然后你就可以更新新行该对象并重新绑定以往时候你做出改变。
You should pull your data into a DataTable and/or DataSet and store it in a Session object. Then you can update this object with new rows and rebind ever time you make a change.
例如:
pageLoad的
if (!IsPostBack)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand("SELECT Id, Name, Price FROM MyCart;", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
Session["MyData"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
现在你有一个数据表绑定到GridView1,你有源数据的副本在你的会话对象。现在,让我们添加项目到GridView并更新了我们对会话对象DT
Now you have a datatable bound to your GridView1 and you have a copy of the source data in your session object. Now, lets add an item to the gridview and update our dt in our session object.
DataTable dt = new DataTable();
if (Session["MyData"] != null)
{
dt = (DataTable)Session["MyData"];
}
DataRow dr = dt.NewRow();
dr["Id"] = dt.Rows.Count + 1;
dr["Name"] = "My New Item";
dr["Price"] = 19.99;
dt.Rows.Add(dr);
Session["MyData"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
有你走,你添加一个项目到您的数据表,它rebinded到你的GridView。
There you go, you've added an item to your datatable and rebinded it to your GridView.
如果你想要做的这一切客户端,你可以轻松地添加使用jQuery一表行,但你不得不如果你需要它在多页坚持表/数据。
If you want to do this all client side, you could easily add a table row using jQuery but you'd have to persist the table/data if you need it on multiple pages.