ASP.NET GridView默认排序顺序

Dav*_*den 5 asp.net

我有一个绑定到SQL数据源的简单gridview控件.现在我启用了排序,但是当我点击要排序的列时,它会先按升序排序.当我再次单击同一列时,它会按降序排序.我想转换它.我想让它在第一次点击时降序Descending,然后升序第二次.我怎么做?

这是我的Gridview控件代码:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
        AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" 
        BorderStyle="Solid" BorderWidth="1px" CellPadding="3" 
        DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" >
        <AlternatingRowStyle BackColor="#CCCCCC" />
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="Team" HeaderText="Team" SortExpression="Team" />
            <asp:BoundField DataField="Matches" HeaderText="Matches" 
                SortExpression="Matches" />
            <asp:BoundField DataField="Points" HeaderText="Points" 
                SortExpression="Points" />
            <asp:BoundField DataField="Tries" HeaderText="Tries" SortExpression="Tries" />
            <asp:BoundField DataField="Conversions" HeaderText="Conversions" 
                SortExpression="Conversions"  />
            <asp:BoundField DataField="Penalties" HeaderText="Penalties" 
                SortExpression="Penalties" />
            <asp:BoundField DataField="Drop Goals" HeaderText="Drop Goals" 
                SortExpression="Drop Goals" />
            <asp:BoundField DataField="Yellow Cards" HeaderText="Yellow Cards" 
                SortExpression="Yellow Cards" />
            <asp:BoundField DataField="Red Cards" HeaderText="Red Cards" 
                SortExpression="Red Cards" />
        </Columns>
        <FooterStyle BackColor="#CCCCCC" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#808080" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#383838" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT * FROM [statstable]"></asp:SqlDataSource> 
Run Code Online (Sandbox Code Playgroud)

ski*_*are 13

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Set your deault sort expression and direction.
        if (String.IsNullOrEmpty(MyGridView.SortExpression)) MyGridView.Sort("SortExpression", SortDirection.Ascending);
    }
}
Run Code Online (Sandbox Code Playgroud)


Joe*_*uso 0

我建议您查看这篇文章并反转升序/降序标志逻辑。

GridView 排序:SortDirection 始终升序

你的 ASPX 页面将有一个像这样的 gridview (特别注意 OnSorting 部分):

<asp:GridView ID="grdHeader" AllowSorting="true" AllowPaging="false" 
   AutoGenerateColumns="false" Width="780" runat="server" 
   OnSorting="grdHeader_OnSorting" EnableViewState="true">
   <Columns>         
      <asp:BoundField DataField="Entitycode" HeaderText="Entity" SortExpression="Entitycode" />        
      <asp:BoundField DataField="Statusname" HeaderText="Status" SortExpression="Statusname" />         
      <asp:BoundField DataField="Username" HeaderText="User" SortExpression="Username" />     
   </Columns> 
 </asp:GridView> 
Run Code Online (Sandbox Code Playgroud)

然后,您的 CodeBehind (aspx.cs) 将需要实现 grdHeader_OnSorting 方法。GridViewSortEventArgs 中的变量将告诉您用户要求排序的方向。在你的情况下,你会做相反的事情......例如:

protected void grdHeader_OnSorting(object sender, GridViewSortEventArgs e)        
{               
   List<V_ReportPeriodStatusEntity> items = GetPeriodStatusesForScreenSelection();
   SortDirection dir = e.SortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
   items.Sort(new Helpers.GenericComparer<V_ReportPeriodStatusEntity>(e.SortExpression, dir));
   grdHeader.DataSource = items;     
   grdHeader.DataBind(); 
} 
Run Code Online (Sandbox Code Playgroud)

这里 items.Sort 方法正在执行所有排序...您所做的就是获取排序结果并将其分配回数据源。

如果您想了解有关如何对 GridView 进行排序的概念的更多信息,我建议您查看 MSDN 上的此示例: http: //msdn.microsoft.com/en-us/library/system.web.ui.webcontrols。 gridview.sort.aspx

如果还有什么我可以澄清的,请告诉我。