当行编辑时,asp.net Gridview排序消失

本文关键字:net Gridview 排序 消失 asp 编辑 | 更新日期: 2023-09-27 18:10:41

当我点击行更新时,gridview消失并返回到其初始状态。可能是什么?下面抄写我的代码:

  private string GetSortDirection(string column)
    {
        // By default, set the sort direction to ascending.
        string sortDirection = "ASC";
        // Retrieve the last column that was sorted.
        string sortExpression = ViewState["SortExpression"] as string;
        if (sortExpression != null)
        {
            // Check if the same column is being sorted.
            // Otherwise, the default value can be returned.
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"] as string;
                if ((lastDirection != null) && (lastDirection == "ASC"))
                {
                    sortDirection = "DESC";
                }
            }
        }
        // Save new values in ViewState.
        ViewState["SortDirection"] = sortDirection;
        ViewState["SortExpression"] = column;
        return sortDirection;
    }
    protected void GridViewLayoutProduto_Sorting(object sender, GridViewSortEventArgs e)
    {
        //Retrieve the table from the session object.
        DataTable dt = Session["TaskTable"] as DataTable;
        if (dt != null)
        {
            //Sort the data.
            dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
            Session["TaskTable"] = dt;
            GridViewLayoutProduto.DataSource = Session["TaskTable"];
            GridViewLayoutProduto.DataBind();
        }
    }
    protected void GridViewLayoutProduto_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridViewLayoutProduto.EditIndex = e.NewEditIndex;
        //CheckBox alt = GridViewLayoutProduto.Rows[e.NewEditIndex].FindControl("CheckBox1") as CheckBox;
        //alt.Enabled = false;
        BindData();
    }
    protected void GridViewLayoutProduto_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewLayoutProduto.PageIndex = e.NewPageIndex;
        GridViewLayoutProduto.DataBind();
    }
    protected void GridViewLayoutProduto_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridViewLayoutProduto.EditIndex = -1;
        BindData();
    }
    private void BindData()
    {
        GridViewLayoutProduto.DataSource = Session["TaskTable"];
        GridViewLayoutProduto.DataBind();
    }

当行编辑时,asp.net Gridview排序消失

在会话数据中要存储的数据表没有被组织,我如何在会话中存储已执行过数据表排序的数据表呢?

试着把这行放在Sort of GridView

之后
Session["TaskTable"] = ((DataView)gvProgramlar.DataSource).ToTable();

希望这对你有帮助!