动态网格问题 [英] Dynamic grid problem
本文介绍了动态网格问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有模板控件的动态网格.我有两个按钮:添加"和删除".当我绑定网格并单击添加"按钮时,最后一行数据将显示在网格中,所有数据都将被删除,并创建一个空白行.
这是我的代码:
I have a dynamic grid with a template control. I have two buttons: ADD and REMOVE. When I bind the grid and click on the Add button, the last row data is displayed in the grid and all data is removed, and a blank row is created.
Here is my code:
int rowIndex = 0;
if (ViewState["CurrentTable1"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable1"];
DataTable DtLast = null;
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].
Cells[1].FindControl("TxtName");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].
Cells[2].FindControl("TxtStatus");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].
Cells[3].FindControl("TxtAddress");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["SrNo"] = i + 1;
drCurrentRow["Name"] = box1.Text;
drCurrentRow["Status"] = box2.Text;
drCurrentRow["Address"] = box3.Text;
rowIndex++;
DtLast.Rows.Add(drCurrentRow);
}
//add new row to DataTable
//Store the current data to ViewState
ViewState["CurrentTable1"] = dtCurrentTable;
//Rebind the Grid with the current data
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
有人可以帮我吗?
Can anyone please help me?
推荐答案
首先,您不应该使用Viewstate在其中存储datagrid数据.这会使页面变得很沉重,从而影响性能.
而是使用缓存.
更改后,您需要重新绑定到源.要么再次获取数据,要么在缓存/数据集中进行必要的更改,然后将其重新绑定到网格.
In first place, you should not use Viewstate to store datagrid data in it. It will make page a lot heavy and this will hit performance.
Instead use Caching.
You need a rebind to the source after the change. Either fetch the data again or make the necessary change in the cache/dataset and rebind it to the grid.
您要存储的&使用来自Viewstate
的相同Datatable [ dtCurrentTable ]重新绑定网格
因此存储&绑定数据表 DtLast
you are storing & rebinding grid with the same Datatable[dtCurrentTable] which is from Viewstate
so store & bind the Datatable DtLast
//Store the current data to ViewState
ViewState["CurrentTable1"] = DtLast;
//Rebind the Grid with the current data
Gridview1.DataSource = DtLast;
Gridview1.DataBind();
这篇关于动态网格问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文