asp GridView没有绑定

时间:2017-03-07 05:07:43

标签: c# asp.net gridview

我的代码问题是我尝试更新数据表(删除几行),然后尝试将其分配给网格视图数据源。数据表正在更新,并在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);
                }

            }

        }

1 个答案:

答案 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;

       }