GridView系列(一)排序列头加箭头
GridView.aspx
<%@ 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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div >
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1"
OnRowCreated="GridView1_RowCreated" OnSorting="GridView1_Sorting" PageSize="5">
<Columns>
<asp:BoundField DataField="CompanyName" HeaderText="公司名称" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="联系人" SortExpression="ContactName" />
<asp:BoundField DataField="Address" HeaderText="地址" SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="城市" SortExpression="City" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Customers]"></asp:SqlDataSource>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<br />
d</div>
</form>
</body>
</html>
<!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" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1"
OnRowCreated="GridView1_RowCreated" OnSorting="GridView1_Sorting" PageSize="5">
<Columns>
<asp:BoundField DataField="CompanyName" HeaderText="公司名称" SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="联系人" SortExpression="ContactName" />
<asp:BoundField DataField="Address" HeaderText="地址" SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="城市" SortExpression="City" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Customers]"></asp:SqlDataSource>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<br />
d</div>
</form>
</body>
</html>
GridView.aspx.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)
{
//这种方法直接新加一个控件,也可以换成图片
//Label ls = new Label();
//ls.Font.Name = "Webdings";
//ls.Text = "5";
//tc.Controls.Add(ls);
if (tc.Controls.Count > 0 )//这里要判断一下此时是不是已经生成了linkbutton
string s1 = ((LinkButton)tc.Controls[0]).Text;
((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)
{
//Label l = new Label();
//l.Font.Name = "Webdings";
//if (sortdir == "Ascending")
//{
// l.Text = "5";
//}
//else
//{
// l.Text = "6";
//}
//tc.Controls.Remove(ls);
//tc.Controls.Add(l);
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();
}
}
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)
{
//这种方法直接新加一个控件,也可以换成图片
//Label ls = new Label();
//ls.Font.Name = "Webdings";
//ls.Text = "5";
//tc.Controls.Add(ls);
if (tc.Controls.Count > 0 )//这里要判断一下此时是不是已经生成了linkbutton
string s1 = ((LinkButton)tc.Controls[0]).Text;
((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)
{
//Label l = new Label();
//l.Font.Name = "Webdings";
//if (sortdir == "Ascending")
//{
// l.Text = "5";
//}
//else
//{
// l.Text = "6";
//}
//tc.Controls.Remove(ls);
//tc.Controls.Add(l);
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();
}
}