Try putting the SQL Statement, including the ORDER BY clause into ViewState.
Before you bind the DataGrid, you have to recreate, or at least resort the
data source. Put the sort values into ViewState, so they are available when
you page.
[CODE BEHIND]
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public class DataGrid001 : Page
{
protected DataGrid myGrid;
const string SqlStmt = "SELECT * FROM Customers";
protected void Page_Load(object s, EventArgs e)
{
if(!Page.IsPostBack)
{
ViewState["Sql"] = SqlStmt;
BindGrid();
}
}
private void BindGrid()
{
SqlDataAdapter myAdapter
new SqlDataAdapter(
ViewState["Sql"].ToString(),
ConfigurationSettings.AppSettings["NWDconString"]
);
DataSet myDataSet = new DataSet();
myAdapter.Fill(myDataSet);
myGrid.DataSource = myDataSet;
myGrid.DataBind();
}
protected void myGrid_OnSortCommand(object s, DataGridSortCommandEventArgs
e)
{
ViewState["Sql"] = SqlStmt + " ORDER BY " + e.SortExpression;
BindGrid();
}
protected void myGrid_OnPageChanged(object s, DataGridPageChangedEventArgs
e)
{
myGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
}
[WEB FORM]
<% @ Page INHERITS="DataGrid001" Src="DataGrid001.aspx.cs" %>
<html>
<body bgcolor="white">
<form runat="server">
<asp:DataGrid runat="server"
ID="myGrid"
AllowPaging="True"
AllowSorting="True"
PagerStyle-Mode="NumericPages"
OnSortCommand="myGrid_OnSortCommand"
OnPageIndexChanged="myGrid_OnPageChanged"
/>
</form>
</body>
</html>
(the standard DataGrid has sorting and paging capabilities.)
______________________________________________
doug seven | sr. developer | dotnetjunkies.com
Learn ASP.NET - Read the books...
Programming Data-Driven Web Applications with ASP.NET
http://www.amazon.com/exec/obidos/ASIN/0672321068/dotnetjunkies-20/107-10642
54-2553318
ASP.NET: Tips, Tutorials and Code
http://www.amazon.com/exec/obidos/ASIN/0672321432/dotnetjunkies-20/107-10642
54-2553318
> -----Original Message-----
> From: philip.moh@a... [mailto:philip.moh@a...]
> Sent: Wednesday, September 26, 2001 5:12 PM
> To: ASPX_Professional
> Subject: [aspx_professional] RE: datagrids: sorting and paging issue
>
>
> can I know what datagrid are you using?? I am also looking for the third
> party datagrid...
>
> > -----Original Message-----
> > From: slwk75@y... [SMTP:slwk75@y...]
> > Sent: Thursday, September 27, 2001 2:51 AM
> > To: ASPX_Professional
> > Subject: [aspx_professional] datagrids: sorting and paging issue
> >
> > I have a datagrid that offers sorting and paging.
> >
> > Let's say the following is the grid:
> >
> > Col1 Col2
> > 1 B
> > 2 C
> > 3 A
> >
> > Let's say I sort by Col2. The result is:
> >
> > Col1 Col2
> > 3 A
> > 1 B
> > 2 C
> >
> > Let's say there is one row per page and now I move to the next
> page. What
> >
> > happens is the datagrid changes pages based on the original sorting of
> > Col1 and not on the latest sorting of Col2, which is the effect
> I'm after.
> >
> > How can I solve this problem?
> >
> > Thanks in advance.