gridview 排序加箭头(一)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView.aspx.cs" Inherits="GridView" %>
<!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 id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div >
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
OnRowCreated="GridView1_RowCreated" OnSorting="GridView1_Sorting" PageSize="5">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
<asp:BoundField DataField="areaID" HeaderText="areaID" SortExpression="areaID" />
<asp:BoundField DataField="area" HeaderText="area" SortExpression="area" />
<asp:BoundField DataField="father" HeaderText="father" SortExpression="father" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dsc110874_dbConnectionString %>"
SelectCommand="SELECT * FROM [Area]"></asp:SqlDataSource>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<br />
d</div>
</form>
</body>
</html>
cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class GridView : System.Web.UI.Page
{
string sortexp = "";//排序表达式
string sortdir = "";//排序方向
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell tc in e.Row.Cells)
{
//这种方法直接新加一个控件,也可以换成图片
if (tc.Controls.Count > 0)//这里要判断一下此时是不是已经生成了linkbutton
{
string s1 = ((LinkButton)tc.Controls[0]).Text;
//样式Webdings是123都有自己对应的图片
((LinkButton)tc.Controls[0]).Text = s1.Replace(s1, s1 + "<font face='Webdings'>5</font>");
//倒序的样式
if (tc.Controls.Count > 0 && tc.Controls[0].GetType().ToString() == "System.Web.UI.WebControls.DataControlLinkButton")
{
if (((LinkButton)tc.Controls[0]).CommandArgument == sortexp)
{
string s2 = ((LinkButton)tc.Controls[0]).Text;
if (sortdir == "Descending")
{
((LinkButton)tc.Controls[0]).Text = s2.Replace("5", "6");
}
}
}
}
}
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
sortexp = e.SortExpression.ToString();
sortdir = e.SortDirection.ToString();
}
}