ASP.NET GridView默认排序顺序 [英] ASP.NET GridView Default Sorting Order
本文介绍了ASP.NET GridView默认排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个绑定到sql数据源的简单gridview控件.现在,我启用了排序,但是当我单击要排序的列时,它将首先以升序对它进行排序.当我再次单击同一列时,它以降序排列.我想改变一下.我希望它在第一次单击时按降序排序,在第二次单击时按升序排序.我该怎么办?
I have a simple gridview control bound to an sql datasource. Now I enabled sorting, but when I click on a column to be sorted, it sorts it in an Ascending Order first. When I click the same column again, it sorts it in a Descending order. I want to switch that around. I want it to sort Descending on the first click, and Ascending the second. How do I do that?
这是我的Gridview控件代码:
Here is my Gridview Control code:
<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>
推荐答案
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);
}
}
这篇关于ASP.NET GridView默认排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文