Gridview Asp NET disappears Sort after Click to perform Update

Asked

Viewed 183 times

1

I have my Gridview developed manually, the problem and that when I click to carry out the update my gridview ceases to be orderly and returns to its normal state. I intended that this would not happen.

Here I demonstrate how it is defined:

 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 lies in Session["Tasktable"] because I always keep the original state of Grid I think.

1 answer

1


According to your code, after Sort, the session datatable is out of date. Any event that reloads returns to initial state.

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

In the event Gridviewlayoutproduto_sorting Voce is placing the datatable from the section to the ordered datatable instead.

 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();
    }
  • dt.Totable does not exist at least gives me error.

  • I corrected it. I edited the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.