<%@ Page language="c#" Inherits="DataViewSort" CodeFile="Default.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<body>
<form method="post" runat="server" ID="Form1">
<b><u>Original order</u></b><br>
<br>
<asp:GridView runat="server" ID="Datagrid1" HeaderStyle-Font-Bold="true" />
<br>
<b><u>Sort = "LastName"</u></b><br>
<br>
<asp:GridView runat="server" ID="Datagrid2" HeaderStyle-Font-Bold="true" />
<br>
<b><u>Sort = "FirstName"</u></b><br>
<br>
<asp:GridView runat="server" ID="Datagrid3" HeaderStyle-Font-Bold="true" />
</form>
</body>
</HTML>
File: Default.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class DataViewSort : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
string connectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI";
SqlConnection con = new SqlConnection(connectionString);
string sql = "SELECT TOP 5 EmployeeID, TitleOfCourtesy, LastName, FirstName FROM Employees";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds, "Employees");
Datagrid1.DataSource = ds.Tables["Employees"];
DataView view2 = new DataView(ds.Tables["Employees"]);
view2.Sort = "LastName";
Datagrid2.DataSource = view2;
DataView view3 = new DataView(ds.Tables["Employees"]);
view3.Sort = "FirstName";
Datagrid3.DataSource = view3;
this.DataBind();
}
}