<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat=server>
DataView dvwProducts;
void Page_Load(Object sender , EventArgs e)
{
SqlConnection conNorthwind;
SqlDataAdapter dadProducts;
DataSet dstProducts;
// Create Products DataSet
dstProducts = new DataSet();
conNorthwind = new SqlConnection("server=IPROMISE;database=Northwind;uid=sa;password=612121tjy;");
dadProducts = new SqlDataAdapter( "Select * From Products", conNorthwind );
dadProducts.Fill( dstProducts, "Products" );
// Create default DataView
dvwProducts = dstProducts.Tables[ "Products"].DefaultView;
// Bind to datagrid
dgrdProducts.DataSource = dstProducts;
dgrdProducts.DataBind();
}
void dgrdProducts_SortCommand( object s, DataGridSortCommandEventArgs e ) {
// Sort DataView
dvwProducts.Sort = e.SortExpression;
// Rebind to DataGrid
dgrdProducts.DataSource = dvwProducts;
dgrdProducts.DataBind();
}
</Script>
<html>
<head><title>SortDataView.aspx</title></head>
<body>
<form Runat="Server">
<asp:DataGrid
ID="dgrdProducts"
AllowSorting="True"
OnSortCommand="dgrdProducts_SortCommand"
Runat="Server" />
</form>
</body>
</html>