GridView控件有双向排序功能.以前很少用到.今天使用到了.发现功能很强大.故在些标记,以备忘!
效果好下:
前台代码:
<asp:GridView ID="UserGridList" runat="server" AllowPaging="True" AutoGenerateColumns="False" OnRowDataBound="UserGridList_RowDataBound" CellPadding="4" ForeColor="#333333" PageSize="15" onpageindexchanging="UserGridList_PageIndexChanging" AllowSorting="True" OnSorting="UserGridList_Sorting"> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#EFF3FB" /> <Columns> <asp:BoundField DataField="ID" HeaderText="编号" InsertVisible="False" ReadOnly="True" SortExpression="ID"> <HeaderStyle HorizontalAlign="Center" Width="60px" /> <ItemStyle HorizontalAlign="Center" Width="60px" /> </asp:BoundField> <asp:TemplateField ShowHeader="False" HeaderText="↑面试提醒↓" SortExpression="InterTime"> <%--<HeaderTemplate> </HeaderTemplate>--%> <ItemTemplate> <%# Eval("InterTime") %> </ItemTemplate> <HeaderStyle HorizontalAlign="Center" Width="100px" /> <ItemStyle HorizontalAlign="Center" Width="100px" /> </asp:TemplateField> </Columns> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <EmptyDataTemplate> <table border="1" dir="ltr" style="border: 1px solid #FFFFFF; width: 750px; border-collapse: collapse; text-align: center;"> <tr style="background-color: #507CD1; color: #FFFFFF;" mce_style="background-color: #507CD1; color: #FFFFFF;"> <td style="width: 60px; height: 25px;"> 编号 </td> <td style="width: 400px"> 描述 </td> <td style="width: 90px"> 添加时间 </td> <td style="width: 50px"> </td> <td style="width: 50px"> </td> </tr> <tr> <td colspan="6" style="height: 40px; color: #FF0000;"> 没有相关数据! </td> </tr> </table> </EmptyDataTemplate> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#2461BF" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView>
后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState["SortInterTime"] = "ID"; ViewState["OrderDire"] = "Desc"; bindGV(); } } protected void bindGV() { string sqlstr = ""; string AscStr = ""; AscStr = ViewState["SortInterTime"].ToString() + " " + ViewState["OrderDire"].ToString(); sqlstr = "SELECT ID,QTable from users where ID=UID order by " + AscStr + ""; DataTable dt = DtDB.GetDataTable(sqlstr); UserGridList.DataSource = dt; UserGridList.DataBind(); dt.Dispose(); } //GV排序事件 protected void UserGridList_Sorting(object sender, GridViewSortEventArgs e) { string spage = e.SortExpression; //DtDB.AlertForPage(Page, ViewState["SortInterTime"].ToString()); //ViewState["SortInterTime"] = "InterTime"; //ViewState["OrderDire"] = "ASC"; if (ViewState["SortInterTime"].ToString() == spage) { if (ViewState["OrderDire"].ToString() == "desc") { ViewState["OrderDire"] = "asc"; } else { ViewState["OrderDire"] = "desc"; } } else { ViewState["SortInterTime"] =e.SortExpression; } bindGV(); }