user1084683
user1084683

Reputation:

GridView Sorting resets page to original data

H guys, I am using VS2005 C# and SQL Server 2005.

I have a search for my GridView and I would like to use the Sort function of the GridView to sort my search results.

However, after every time I have my results filtered, when I press the header to sort my DataGrid, it always reset my GridView back to the original full data from the table.

E.g.

a) My webpage loads, a gridview containing data of 58 students.

b) I search for James under Names and 18 results are displayed in the GridView.

c) I press on the header Class to sort results by class.

d) The GridView refreshes and goes back to original full list of 58 students.


I tried:

  1. Implementing the search table on a seperate webpage so it would not collide with the original gridview.

  2. Changing the gridview name to another name.


I realize that:

When I hover my pointer above the header, it will always display javascript:_doPostBack('ctl00$MainContent$GridView1,Sort$Issue),

even though I may have changed my GridView1 to another name


I need some solution in sorting search results in gridview, instead of bringing the original data back when I sort them.

Upvotes: 2

Views: 2239

Answers (2)

Jignesh Rajput
Jignesh Rajput

Reputation: 3558

suppose when you call gridview for Bind in Page load

  protected void Page_Load(object sender, EventArgs e)
    {  
         fillgrid(false, string.Empty, false); //
    }

  public void fillgrid(bool sorting, string sortexpression, bool sortdir)
    {
          //binding codes 
           var data = from item in DBcontent.Tablename 
                      select new
                      {
                          Title = item.Title
                      }  
             if (sorting)
                    {
                        if (sortexpression == "Title")
                        {
                            if (sortdir)
                            {
                                GrieviewID.DataSource = data.OrderBy(id => id.Title).ToList();
                            }
                            else
                            {
                                GrieviewID.DataSource = data.OrderByDescending(id => id.Title).ToList();
                            }
                        }
                   else
                    {
                        GrdID.DataSource = data.OrderByDescending(id => id.StartDate).ToList();
                    }
                GrdID.DataBind();
    }

  protected void grdevents_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdevents.PageIndex = e.NewPageIndex;
        BindAndSortGrid();
    }

    /// <summary>
    /// Gets the sort direction.
    /// </summary>
    /// <param name="column">The column.</param>
    /// <returns></returns>
    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;
    }

    /// <summary>
    /// Bind and sort grid.
    /// </summary>
    private void BindAndSortGrid()
    {
        bool sortdir;
        if (ViewState["SortDirection"] != null && ViewState["SortExpression"] != null)
        {
            sortdir = ViewState["SortDirection"].ToString() == "ASC" ? true : false;
            fillgrid(true, ViewState["SortExpression"].ToString(), sortdir);
        }
        else
            fillgrid(false, string.Empty, false);
    }

    protected void grdevents_Sorting(object sender, GridViewSortEventArgs e)
    {
        bool sortdir = GetSortDirection(e.SortExpression) == "ASC" ? true : false;
        fillgrid(true, e.SortExpression.ToString(), sortdir);
    }

Follow below code...Hopes its help

Upvotes: 0

aleafonso
aleafonso

Reputation: 2256

I have implemented that using the ViewState to store the sort direction, either ASC or DESC in the following way:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        listBindByName(); //this would be your procedure to look for the data you want
        DataSet dsSortTable = GridView1.DataSource as DataSet;
        DataTable dtSortTable = dsSortTable.Tables[0];
        if (dtSortTable != null)
        {
            DataView dvSortedView = new DataView(dtSortTable);
            dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString();
            ViewState["sortExpression"] = e.SortExpression;
            GridView1.DataSource = dvSortedView;
            GridView1.DataBind();
        }
        UpdatePanel1.Update();
    }

    private string getSortDirectionString()
    {
        if (ViewState["sortDirection"] == null)
        {
            ViewState["sortDirection"] = "ASC";
        }
        else
        {
            if (ViewState["sortDirection"].ToString() == "ASC")
            {
                ViewState["sortDirection"] = "DESC";
                return ViewState["sortDirection"].ToString();
            }
            if (ViewState["sortDirection"].ToString() == "DESC")
            {
                ViewState["sortDirection"] = "ASC";
                return ViewState["sortDirection"].ToString();
            }
        }
        return ViewState["sortDirection"].ToString();
    }

Let me know if any doubt.

Upvotes: 1

Related Questions