asp.net网格视图问题

问题描述:

我有一个网格视图,并按数据表填充此网格视图,
但是一段时间后,我想在现有的网格视图上添加新数据
我如何在不干扰gridview的旧状态的情况下从数据表在网格视图上添加新数据
请帮助我
在此先感谢

I have a grid view and i populate this grid view by data table,
but after some time i want to add new data on the existing grid view
how i add new data on the grid view from the datatable without disturbing the old state of gridview
please help me
thanks in advance

只需存储您的数据表为Session变量,并将新行添加到数据表并将其作为数据源提供给您的gridview.

Just store your datatable is Session variable and add new row to datatable and give it as datasource to your gridview.

DataTable dt = null;
try
{
    if (Session["dtItems"] != null)
    {
        dt = (DataTable)Session["dtItems"];
    }
    else
    {
        dt = new DataTable();
        dt.Columns.Add("Quantity");
        dt.Columns.Add("Rate_Unit");
        dt.Columns.Add("Amount");
    }
    DataRow dataRow;
    dataRow = dt.NewRow();
    dataRow["Quantity"] = TextBox1.Text;
    dataRow["Rate_Unit"] = TextBox2.Text;
    dataRow["Amount"] = TextBox3.Text;
    dt.Rows.Add(dataRow);
    dt.AcceptChanges();
    Session["dtItems"] = dt;
    GridView1.DataSource = dt.DefaultView;
    GridView1.DataBind();
}
catch (Exception ex)
{
}