Wednesday, February 15, 2012

sorting in gridview

if datasourcecontrol is not used, you have to handle sorting and paging by yourself in on_paging or on_sorting
public string GetSortDirection(string SortExpression)
 {
    if (ViewState[SortExpression] == null)
        ViewState[SortExpression] = "Desc";
    else
        ViewState[SortExpression] = ViewState[SortExpression].ToString() == "Desc" ? "Asc" : "Desc";

     return ViewState[SortExpression].ToString();
  }

  protected void GridView_OnSorting(object sender, GridViewSortEventArgs e)
 {
    string order = GetSortDirection(e.SortExpression);
    string s = "select * from [User] order by " + e.SortExpression + " " + order;
    BindGrid(s);
 }

 void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            ((GridView)sender).PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }

No comments:

Post a Comment