//////////////////////.aspx代码////////////////////////
<div>
<asp:GridView ID="gv_pro" runat="server" AllowPaging="true" PageSize="10" PagerSettings-Mode="Numeric" AllowSorting="true" Font-Size="12px" Width="760" AutoGenerateColumns="false" CellPadding="4" OnPageIndexChanging="gv_pro_PageIndexChanging" OnSorting="gv_pro_Sorting"
>
<HeaderStyle BackColor="#006699" ForeColor="#ffffff" />
<Columns>
<asp:BoundField DataField="pro_name" HeaderText="产品名称" ItemStyle-Height="22" ItemStyle-Width="300" SortExpression="pro_name" />
<asp:BoundField DataField="pro_jianjie" HeaderText="产品简介" ItemStyle-Width="200" SortExpression="pro_jianjie"/>
<asp:BoundField DataField="pro_date" HeaderText="添加时间" SortExpression="pro_date"/>
</Columns>
</asp:GridView>
</div>
//////////////.cs代码////////////////////////////////
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
{
/// <summary>
/// 绑定到GridView
/// </summary>
private void bingclass()
{
//实例化sqlconnection对象
SqlConnection conn = new SqlConnection();
//从webconfig中取得数据库连接字符串
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["yfmy"].ConnectionString;
//实例化sqlcommand对象
SqlCommand cmd = new SqlCommand();
//设置查询语句
cmd.CommandText = "select pro_name,pro_jianjie,pro_date from yf_pro";
//给cmd对象建立数据库的连接
cmd.Connection = conn;
//打开链接到的数据库
conn.Open();
//实例化sqldataadapter
SqlDataAdapter sda = new SqlDataAdapter();
//设置sqldataAdapter的查询语句
sda.SelectCommand = cmd;
//实例化dataset
DataSet ds = new DataSet("pro");
//将结果填充到dataset中
sda.Fill(ds);
//关闭数据库连接
conn.Close();
//实例化datatable
DataTable dt = new DataTable();
//取得内存中的数据集
dt = ds.Tables[0];
//取得排序的字段
string sortExpression = this.gv_pro.Attributes["SortExpression"];
//取得排序的顺序
string sortDirection = this.gv_pro.Attributes["SortDirection"];
//判断如果排序的字段和顺序都不为空
if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
{
//设置视图中的排序的字段以及排序的顺序
dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
}
//设置gridview的数据源
this.gv_pro.DataSource = dt.DefaultView;
//绑定
this.gv_pro.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
//如果不是回传的话
if (!IsPostBack)
{
//设定gridview的初始排序字段以及顺序
this.gv_pro.Attributes.Add("SortExpression", "pro_date");
this.gv_pro.Attributes.Add("SortDirection", "ASC");
//绑定
bingclass();
}
}
protected void gv_pro_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//获取当前是第几页
this.gv_pro.PageIndex = e.NewPageIndex;
//重新绑定
bingclass();
}
protected void gv_pro_Sorting(object sender, GridViewSortEventArgs e)
{
//获得当前点击的列
string sortExpression = e.SortExpression.ToString();
string sortDirection = "ASC";
if (sortExpression == this.gv_pro.Attributes["SortExpression"])
{
//获得下一次的排序状态
sortDirection = (this.gv_pro.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
}
// 重新设定GridView排序数据列及排序方向
this.gv_pro.Attributes["SortExpression"] = sortExpression;
this.gv_pro.Attributes["SortDirection"] = sortDirection;
//重新绑定
bingclass();
}
}