gridview自定义排序
效果如图:
首先允许排序:AllowSorting="True";开启gridview的排序事件onsorting="GridView1_Sorting",也可以双击sorting事件;其次是设置nrowdatabound="GridView1_RowDataBound",也可以双击GridView1_RowDataBound
关联排序表达式SortExpression="字段名称"
代码明细:
前台代码:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<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"
CellPadding="4" ForeColor="#333333" Width="800px"
AllowSorting="True"
onrowdatabound="GridView1_RowDataBound" onsorting="GridView1_Sorting"
PageSize="15">
<PagerSettings Mode="NumericFirstLast"/>
<RowStyle BackColor="#EFF3FB"/>
<Columns>
<asp:BoundField DataField="CourseResID" HeaderText="课程ID"/>
<asp:BoundField DataField="CourseName" HeaderText="课程名" SortExpression="CourseName">
<ItemStyle Width="100px"/>
</asp:BoundField>
<asp:BoundField DataField="upTime" HeaderText="更新时间"
DataFormatString="{0:yyyy-MM-dd}" SortExpression="upTime">
<ItemStyle Width="160px" HorizontalAlign="Right"/>
</asp:BoundField>
<asp:BoundField DataField="UserName" HeaderText="用户名" SortExpression="UserName">
<ItemStyle Width="80px" ForeColor="#33CC33"/>
</asp:BoundField>
</Columns>
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" Width="800px"
AllowSorting="True"
onrowdatabound="GridView1_RowDataBound" onsorting="GridView1_Sorting"
PageSize="15">
<PagerSettings Mode="NumericFirstLast"/>
<RowStyle BackColor="#EFF3FB"/>
<Columns>
<asp:BoundField DataField="CourseResID" HeaderText="课程ID"/>
<asp:BoundField DataField="CourseName" HeaderText="课程名" SortExpression="CourseName">
<ItemStyle Width="100px"/>
</asp:BoundField>
<asp:BoundField DataField="upTime" HeaderText="更新时间"
DataFormatString="{0:yyyy-MM-dd}" SortExpression="upTime">
<ItemStyle Width="160px" HorizontalAlign="Right"/>
</asp:BoundField>
<asp:BoundField DataField="UserName" HeaderText="用户名" SortExpression="UserName">
<ItemStyle Width="80px" ForeColor="#33CC33"/>
</asp:BoundField>
</Columns>