我的代码问题是我尝试更新数据表(删除几行),然后尝试将其分配给网格视图数据源。数据表正在更新,并在Gridview.DataSource = dt; (DataSource显示了完美的数据)但Gridview没有得到更新。
我需要在这里添加一些东西吗?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
try
{
GridViewTree1.DataBind();
}
catch (Exception ex)
{
throw;
}
}
GridViewTree1.Visible = true;
GridViewHistory.Visible = false;
dt = Session["dt"] as DataTable;
DeleteParentRowsWithNoChilds(dt);
dt.AcceptChanges();
GridViewTree1.EditIndex = -1;
GridViewTree1.DataSource = dt;
GridViewTree1.DataBind();
}
private void DeleteParentRowsWithNoChilds(DataTable dt)
{
DataRow[] FolderRows;
FolderRows = dt.Select("IsFolder='true'");
foreach (DataRow row in FolderRows)
{
int RowId = int.Parse(row["Id"].ToString());
int nextRowid = RowId + 1;
DataRow[] nextrow = dt.Select("Id='" + nextRowid + "'");
if (nextrow.Count() > 0 && !nextrow[0].ToString().Contains('\\'))
{
dt.Rows.Remove(row);
}
//If it is a last row
else
{
dt.Rows.Remove(row);
}
}
}
答案 0 :(得分:0)
您正在做的是删除rows
但不更新session dt
这就是为什么它始终会获得datatable
中的先前值,因此您应该更新session
之后删除rows
之类的
private void DeleteParentRowsWithNoChilds(DataTable dt)
{
DataRow[] FolderRows;
FolderRows = dt.Select("IsFolder='true'");
foreach (DataRow row in FolderRows)
{
int RowId = int.Parse(row["Id"].ToString());
int nextRowid = RowId + 1;
DataRow[] nextrow = dt.Select("Id='" + nextRowid + "'");
if (nextrow.Count() > 0 && !nextrow[0].ToString().Contains('\\'))
{
dt.Rows.Remove(row);
}
//If it is a last row
else
{
dt.Rows.Remove(row);
}
}
Session["dt"] = dt;
}