Saturday, March 22, 2008

Paging and Sorting in GridView without using Datasource control

 

<asp:GridView ID="gridView" OnPageIndexChanging="gridView_PageIndexChanging" 
OnSorting="gridView_Sorting" runat="server" />
private string ConvertSortDirectionToSql(SortDirection sortDireciton)
{
   string newSortDirection = String.Empty;
   switch (sortDirection)
   {
      case SortDirection.Ascending:
         newSortDirection = "ASC";
         break;
      case SortDirection.Descending:
         newSortDirection = "DESC";
         break;
   }
   return newSortDirection
}
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   gridView.PageIndex = e.NewPageIndex;
   gridView.DataBind();
}
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
   DataTable dataTable = gridView.DataSource as DataTable;
   if (dataTable != null)
   {
      DataView dataView = new DataView(dataTable);
      dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
      gridView.DataSource = dataView;
      gridView.DataBind();
   }
}

Source

4 comments:

  1. The code posted will only sort ascending, since the last used sort direction is not recorded anywhere.

    Here is a modified version that stores the last sort in ViewState:

    [Serializable]
    public struct SortDetails
    {
    public string Expression;
    public SortDirection Direction;

    public string SqlDirection
    {
    get
    {
    if (Direction == SortDirection.Ascending)
    {
    return "ASC";
    }
    else
    {
    return "DESC";
    }
    }
    }
    }
    public SortDetails CurrentSort
    {
    get
    {
    if (ViewState["sortDetails"] == null)
    {
    return new SortDetails();
    }
    return (SortDetails)ViewState["sortDetails"];
    }
    set { ViewState["sortDetails"] = value; }
    }
    protected void EventsGridView_Sorting(object sender, GridViewSortEventArgs e)
    {
    DataView sourceView = EventsGridView.DataSource as DataView;

    if (sourceView != null)
    {
    SortDetails mySort = CurrentSort;
    if (mySort.Expression == e.SortExpression)
    {
    if (mySort.Direction == SortDirection.Ascending)
    {
    mySort.Direction = SortDirection.Descending;
    }
    else
    {
    mySort.Direction = SortDirection.Ascending;
    }
    }
    else
    {
    mySort.Expression = e.SortExpression;
    mySort.Direction = e.SortDirection;
    }
    sourceView.Sort = mySort.Expression + " " + mySort.SqlDirection;
    CurrentSort = mySort;
    EventsGridView.DataSource = sourceView;
    EventsGridView.DataBind();
    }
    }
    Steve
    http://www.appetere.com

    ReplyDelete
  2. I used this code and as far as I can remember, it sorted both ascending and descending...
    I'll check out your solution.
    Thanks

    ReplyDelete
  3. Nice peice of code and thanks for it.

    In my case the following peice of code was always null.

    DataView sourceView = EventsGridView.DataSource as DataView;

    So I wrote some code as follows:
    Project pr = new Project(); //my object
    DataSet dset = pr.GetActiveProjects(); //my object method
    EventsGridView.DataSource = dset;
    DataTable dataTable = dset.Tables[0];
    DataView sourceView = new DataView(dataTable);

    Thereafter the DataView was not null.

    ReplyDelete
  4. I have a GridView with SQLDataSource. I changed the column header text on RowCreated event. Please let me know how can I apply sorting to each column.

    ReplyDelete