DataGrid does not keep sort when I move to another page

Discussion in 'ASP .Net Datagrid Control' started by Richard McClelland, Nov 3, 2004.

  1. I created a DataGrid that allows sorting by several columns. However,
    if the DataGrid spans more than one page, when I navigate to the next
    page the Data is not sorted. Below is PageIndexChanged and
    SortCommand methods. Any help would be much appreciated.


    private void ActivationsList_PageIndexChanged_1(object source,
    System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    ActivationsList.CurrentPageIndex = e.NewPageIndex;

    //Get DataTable of current Session
    DataTable dt = new DataTable();
    dt = (DataTable)Session["Source"];

    // Create a DataView from the DataTable.
    DataView dv = new DataView(dt);
    // Re-bind the data to refresh the DataGrid control.
    ActivationsList.DataSource = dv;
    ActivationsList.DataBind();
    }

    private void ActivationsList_SortCommand(object source,
    System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
    // Retrieve the data source from session state.
    ActivationsList.CurrentPageIndex = 0;
    DataTable dt = new DataTable();
    dt = (DataTable)Session["Source"];


    // Create a DataView from the DataTable.
    DataView dv = new DataView(dt);

    // The DataView provides an easy way to sort. Simply set the
    // Sort property with the name of the field to sort by.
    dv.Sort = e.SortExpression;

    // Rebind the data source and specify that it should be sorted
    // by the field specified in the SortExpression property.
    ActivationsList.DataSource = dv;
    ActivationsList.DataBind();
    Richard McClelland, Nov 3, 2004
    #1
    1. Advertising

  2. Richard McClelland

    Scott M. Guest

    In your SortCommand event handler, you need to store the e.SortExpression
    (the cache or viewstate works well). Then, in your PageIndexChanged event
    handler, you pull the stored SortExpression out, and re-sort the data just
    as you did in the SortCommand event handler. After re-applying the sort,
    change to the correct page index as normal.



    "Richard McClelland" <> wrote in message
    news:...
    >I created a DataGrid that allows sorting by several columns. However,
    > if the DataGrid spans more than one page, when I navigate to the next
    > page the Data is not sorted. Below is PageIndexChanged and
    > SortCommand methods. Any help would be much appreciated.
    >
    >
    > private void ActivationsList_PageIndexChanged_1(object source,
    > System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    > {
    > ActivationsList.CurrentPageIndex = e.NewPageIndex;
    >
    > //Get DataTable of current Session
    > DataTable dt = new DataTable();
    > dt = (DataTable)Session["Source"];
    >
    > // Create a DataView from the DataTable.
    > DataView dv = new DataView(dt);
    > // Re-bind the data to refresh the DataGrid control.
    > ActivationsList.DataSource = dv;
    > ActivationsList.DataBind();
    > }
    >
    > private void ActivationsList_SortCommand(object source,
    > System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    > {
    > // Retrieve the data source from session state.
    > ActivationsList.CurrentPageIndex = 0;
    > DataTable dt = new DataTable();
    > dt = (DataTable)Session["Source"];
    >
    >
    > // Create a DataView from the DataTable.
    > DataView dv = new DataView(dt);
    >
    > // The DataView provides an easy way to sort. Simply set the
    > // Sort property with the name of the field to sort by.
    > dv.Sort = e.SortExpression;
    >
    > // Rebind the data source and specify that it should be sorted
    > // by the field specified in the SortExpression property.
    > ActivationsList.DataSource = dv;
    > ActivationsList.DataBind();
    Scott M., Nov 3, 2004
    #2
    1. Advertising

  3. I'm somewhat a Newbie, so it took me while to figure the ViewState
    out, but once I did it worked perfectly.


    "Scott M." <> wrote in message news:<>...
    > In your SortCommand event handler, you need to store the e.SortExpression
    > (the cache or viewstate works well). Then, in your PageIndexChanged event
    > handler, you pull the stored SortExpression out, and re-sort the data just
    > as you did in the SortCommand event handler. After re-applying the sort,
    > change to the correct page index as normal.
    >
    >
    >
    > "Richard McClelland" <> wrote in message
    > news:...
    > >I created a DataGrid that allows sorting by several columns. However,
    > > if the DataGrid spans more than one page, when I navigate to the next
    > > page the Data is not sorted. Below is PageIndexChanged and
    > > SortCommand methods. Any help would be much appreciated.
    > >
    > >
    > > private void ActivationsList_PageIndexChanged_1(object source,
    > > System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    > > {
    > > ActivationsList.CurrentPageIndex = e.NewPageIndex;
    > >
    > > //Get DataTable of current Session
    > > DataTable dt = new DataTable();
    > > dt = (DataTable)Session["Source"];
    > >
    > > // Create a DataView from the DataTable.
    > > DataView dv = new DataView(dt);
    > > // Re-bind the data to refresh the DataGrid control.
    > > ActivationsList.DataSource = dv;
    > > ActivationsList.DataBind();
    > > }
    > >
    > > private void ActivationsList_SortCommand(object source,
    > > System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    > > {
    > > // Retrieve the data source from session state.
    > > ActivationsList.CurrentPageIndex = 0;
    > > DataTable dt = new DataTable();
    > > dt = (DataTable)Session["Source"];
    > >
    > >
    > > // Create a DataView from the DataTable.
    > > DataView dv = new DataView(dt);
    > >
    > > // The DataView provides an easy way to sort. Simply set the
    > > // Sort property with the name of the field to sort by.
    > > dv.Sort = e.SortExpression;
    > >
    > > // Rebind the data source and specify that it should be sorted
    > > // by the field specified in the SortExpression property.
    > > ActivationsList.DataSource = dv;
    > > ActivationsList.DataBind();
    Richard McClelland, Nov 4, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eugene
    Replies:
    0
    Views:
    475
    Eugene
    Sep 28, 2004
  2. johnjohn
    Replies:
    0
    Views:
    497
    johnjohn
    Sep 6, 2006
  3. sajuptpm
    Replies:
    7
    Views:
    793
    Terry Reedy
    Sep 8, 2010
  4. GoodMorningSky

    How to sort datagrid and keep the bound data as sorted?

    GoodMorningSky, Sep 9, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    141
    GoodMorningSky
    Sep 9, 2004
  5. Navin
    Replies:
    1
    Views:
    668
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page