Gridview Asp NET disappears the Sort after Click to perform Update

1

I have my Gridview developed manually, the problem is that when I click to make update my gridview stops being sorted and returns to normal. I intended this not to happen.

Here's how it's set:

 protected void CarregaGV(DateTime data,int tipo,string tipoSande)
    {
        DataTable dt = null;

        dt = TrataDados.GetDetailsProductLayout(data, tipo,tipoSande);

        Session["TaskTable"] = dt;
        GridViewLayoutProduto.DataSource = dt;
        GridViewLayoutProduto.DataBind();
    }

    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);
            sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
            //Session["TaskTable"] = ((DataView)GridViewLayoutProduto.DataSource).ToTable();
            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();


    }

I think the problem is in Session ["TaskTable"] because I always keep the Grid state in the first place, I think.

    
asked by anonymous 27.06.2014 / 14:23

1 answer

1

According to your code, after sorting, the session datatable becomes outdated. Any event that reloads it back to the initial state.

Anyway, every time LoadDV is called, it will put the Grid in the initial state.

In the GridViewLayoutProducto_Sorting event you are placing the datatable of the section instead of the ordered datatable.

 if (dt != null)
    {
        //Sort the data.

        DataView dv = dt.DefaultView;
        dv.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
        DataTable sortedDT = dv.ToTable();
        Session["TaskTable"] = sortedDT;
        GridViewLayoutProduto.DataSource = sortedDT;
        GridViewLayoutProduto.DataBind();
    }
    
29.06.2014 / 23:03