它可以工作,但问题是我无法保存预览记录,因为如果对象发生数据表重写。
private void AgregarFila()
{
dt.Columns.Add("Fecha", typeof(String));
dt.Columns.Add("Documento", typeof(String));
dt.Columns.Add("Folio", typeof(Int32));
for (int i = 0; i <= 2; i++) // It is only for testing
{
dtRow = dt.NewRow();
dtRow["Fecha"] = Session["HCfecha"];
dtRow["Documento"] = Session["HCDocumento"];
dtRow["Folio"] = Session["HCFolio"];
dt.Rows.Add(dtRow);
}
gvHistoriaLaboral.DataSource = dt;
gvHistoriaLaboral.DataBind();
}
View.aspx
<asp:DataGrid ID="gvHistoriaLaboral" GridLines="None" CssClass="table table-striped" runat="server"></asp:DataGrid>
帮助!
您应该将DataTable存储在Session中,以便以后可以访问,如下所示:
private void AgregarFila()
{
if (Session["miTabla"] == null) //New table, create table and save it with the new record in Session variable
{
dt.Columns.Add("Fecha", typeof(String));
dt.Columns.Add("Documento", typeof(String));
dt.Columns.Add("Folio", typeof(Int32));
dtRow = dt.NewRow();
dtRow["Fecha"] = Session["HCfecha"];
dtRow["Documento"] = Session["HCDocumento"];
dtRow["Folio"] = Session["HCFolio"];
dt.Rows.Add(dtRow);
Session["miTabla"] = dt;
}
else
{
dt = (DataTable) Session["miTabla"]; //Read Session variable
for (int i = 0; i <= 2; i++)
{
dtRow = dt.NewRow();
dtRow["Fecha"] = Session["HCfecha"];
dtRow["Documento"] = Session["HCDocumento"];
dtRow["Folio"] = Session["HCFolio"];
dt.Rows.Add(dtRow);
}
Session["miTabla"] = dt; //Write Session variable after record added
}
gvHistoriaLaboral.DataSource = dt;
gvHistoriaLaboral.DataBind();
}
Session变量值在当前用户会话中保持不变,因此,每当插入新记录时,您只要读取该变量,将其解析为DataTable,添加新记录,然后将DataTable存储回Session中即可。
有关更多信息或其他方式:ASP .NET状态管理
问候!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句