locked
Gridview sorting RRS feed

  • Question

  • User-809753952 posted

    I have a gridview with multiple columns.

    I want to save all column sorting directions to a table. This will help to sort the gridview when the user visits the page next time.

    How can I get all the sort expressions?

    Thursday, September 27, 2018 12:02 PM

All replies

  • User839733648 posted

    Hi mnmhemaj,

    According to your description, I suggest that you should save the sorting directions according to different users.

    You could save into a table which is related to the users.

    How can I get all the sort expressions?

    You may use session to achieve this. You could set and retrieve from Session like the following:

    // Set
    Session["SortExpression"] = "Fname";
    Session["SortDirection"] = (int)SortDirection.Ascending;
    
    // Retrieve
    SortDirection sortDir = (SortDirection)Enum.ToObject(typeof(SortDirection), (int)Session["SortDirection"]);
    string sortExpression = Session["SortField"].ToString(); 

    I've made a sample on my side and hope it will help you to solve your issue. 

    .aspx

        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" AutoGenerateColumns="True" AllowSorting="true" OnSorting="GridView1_Sorting">
                </asp:GridView>
            </div>
        </form>

    code behind.

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGrid();
                }
            }
    
            string constr = ConfigurationManager.ConnectionStrings["EmployeeManagementConnectionString"].ConnectionString;
            private void BindGrid()
            {
                using (SqlConnection con = new SqlConnection(constr))
                {
                    DataTable dt = new DataTable();
                    string myquery = "SELECT * FROM tb_Product";
                    SqlCommand cmd = new SqlCommand(myquery, con);
                    con.Open();
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    sda.Fill(dt);
    
                    //Persist the table in the Session object.
                    Session["ProductsTable"] = dt;
    
                    GridView1.DataSource = Session["ProductsTable"];
                    GridView1.DataBind();
                    con.Close();
                }
            }
    
            protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
            {
                //Retrieve the table from the session object.
                DataTable dt = Session["ProductsTable"] as DataTable;
    
                if (dt != null)
                {
                    //Sort the data.
                    dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
                    GridView1.DataSource = Session["ProductsTable"];
                    GridView1.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;
            }

    Best Regards,

    Jenifer

    Friday, September 28, 2018 8:17 AM