62,074
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="true" OnSorting="GridView1_Sorting">
<Columns>
<asp:BoundField SortExpression="OrderId" DataField="OrderId" HeaderText="定单编号"/>
<asp:BoundField SortExpression="CustomerId" DataField="CustomerId" HeaderText="客户编号"/>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
public const string CONNSTR = "Server=.;Database=NorthWind;UID=sa";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (ViewState["SortField"] == null || ViewState["SortField"].ToString() != "")
ViewState["SortField"] = "OrderId";
DataBind(ViewState["SortField"].ToString());
}
}
public void DataBind(string sortField)
{
string strSql = "SELECT TOP 30 OrderId,CustomerId FROM Orders";
using (SqlConnection conn = new SqlConnection(CONNSTR))
{
conn.Open();
SqlDataAdapter adt = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
adt.Fill(ds);
DataView dvw = ds.Tables[0].DefaultView;
if (!string.IsNullOrEmpty(sortField))
dvw.Sort = sortField;
this.GridView1.DataSource = dvw.Table;
this.GridView1.DataBind();
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState["SortField"].ToString() == e.SortExpression)
ViewState["SortField"] += " DESC";
else
ViewState["SortField"] = e.SortExpression;
DataBind(ViewState["SortField"].ToString());
}
}
<%@ Page language="C#" %>
<script runat="server">
void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
// Use the RowType property to determine whether the
// row being created is the header row.
if (e.Row.RowType == DataControlRowType.Header)
{
// Call the GetSortColumnIndex helper method to determine
// the index of the column being sorted.
int sortColumnIndex = GetSortColumnIndex();
if (sortColumnIndex != -1)
{
// Call the AddSortImage helper method to add
// a sort direction image to the appropriate
// column header.
AddSortImage(sortColumnIndex, e.Row);
}
}
}
// This is a helper method used to determine the index of the
// column being sorted. If no column is being sorted, -1 is returned.
int GetSortColumnIndex()
{
// Iterate through the Columns collection to determine the index
// of the column being sorted.
foreach (DataControlField field in CustomersGridView.Columns)
{
if (field.SortExpression == CustomersGridView.SortExpression)
{
return CustomersGridView.Columns.IndexOf(field);
}
}
return -1;
}
// 这个方法是用来设置排序的图片
void AddSortImage(int columnIndex, GridViewRow headerRow)
{
// 设置排序图片,如果是降序则设置列头为降序图片,否则设置为升序图片
Image sortImage = new Image();
if (CustomersGridView.SortDirection == SortDirection.Ascending)
{
sortImage.ImageUrl = "~/Images/Ascending.jpg";
sortImage.AlternateText = "Ascending Order";
}
else
{
sortImage.ImageUrl = "~/Images/Descending.jpg";
sortImage.AlternateText = "Descending Order";
}
// 在列头添加排序图片
headerRow.Cells[columnIndex].Controls.Add(sortImage);
}
</script>
<html>
<body>
<form runat="server">
<h3>GridView AllowSorting Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="false"
emptydatatext="No data available."
allowsorting="true"
onrowcreated="CustomersGridView_RowCreated"
runat="server">
<columns>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID"
headerstyle-wrap="false"
sortexpression="CustomerID"/>
<asp:boundfield datafield="CompanyName"
headertext="CompanyName"
headerstyle-wrap="false"
sortexpression="CompanyName"/>
<asp:boundfield datafield="Address"
headertext="Address"
headerstyle-wrap="false"
sortexpression="Address"/>
<asp:boundfield datafield="City"
headertext="City"
headerstyle-wrap="false"
sortexpression="City"/>
<asp:boundfield datafield="PostalCode"
headertext="Postal Code"
headerstyle-wrap="false"
sortexpression="PostalCode" />
<asp:boundfield datafield="Country"
headertext="Country"
headerstyle-wrap="false"
sortexpression="Country"/>
</columns>
</asp:gridview>
<!-- 连接到Northwind数据库-->
<!-- 从Web.config读取数据库连接字符串信息. -->
<asp:sqldatasource id="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>