它有效,但问题是我无法保存预览记录'导致数据表在事件对象中重写。
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>
帮助!
答案 0 :(得分:1)
您应该将您的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 State Management
问候!