DataGrid does not keep sort when I move to another page

  • Thread starter Richard McClelland
  • Start date
R

Richard McClelland

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();
 
S

Scott M.

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.
 
R

Richard McClelland

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


Scott M. said:
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 said:
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();
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top