我有一个绑定到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>
答案 0 :(得分:12)
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);
}
}
答案 1 :(得分:3)
为什么容易变得复杂,呃伙计们?
protected void GridView1_OnSorting(object sender, GridViewSortEventArgs e)
{
// If you want to only switch default for some column, disable comment
//switch (e.SortExpression)
//{
// case "MyDataField01":
// case "MyDataField03":
if (e.SortExpression != ((GridView)sender).SortExpression)
{
e.SortDirection = SortDirection.Descending;
}
// break;
// default:
// break;
//}
}
当然,在aspx中的GridView上绑定OnSorting事件。资料来源:http://csc-technicalnotes.blogspot.com/2010/06/change-gridview-sortdirection-default.html
答案 2 :(得分:0)
我建议您查看此帖并反转升序/降序标志逻辑。
GridView sorting: SortDirection always Ascending
您的ASPX页面将具有这样的网格视图(特别关注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>
您的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();
}
这里的items.Sort方法正在进行所有排序......你所做的就是获取排序结果并将其分配回数据源。
如果您想了解有关如何对GridView进行排序的概念的更多信息,我建议您在MSDN上查看此示例: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sort.aspx
如果我还有其他任何事情可以澄清,请告诉我。
答案 3 :(得分:0)
我担心这是默认行为,如果你想改变它,你必须在GridView上实现OnSorting事件并根据排序表达式切换行为。例如,如果排序表达式为TEAM DESC
,那么您将对TEAM ASC
进行排序,反之亦然。
这样的事情(假设您的SelectCommand是一个没有order by子句的简单select语句):
protected void GridView1_Sorting(Object sender, GridViewSortEventArgs e)
{
string originalSelectCommand = SqlDataSource1.SelectCommand;
if (e.SortDirection == "ASC")//sort data source in DESC
{
if(e.SortExpression=="Team")
{
SqlDataSource1.SelectCommand=SqlDataSource1.SelectCommand + " Order BY TEAM DESC";
}
else if(e.SortExpression=="Another Column") //etc
{
}
}
else //Sort data source in ASC
{
}
SqlDataSource1.Select(DataSourceSelectArguments.Empty);
GridView1.DataBind();
SqlDataSource1.SelectCommand=originalSelectCommand;//revert back to original w/o order by
}
您需要对标记执行的操作是:
<asp:GridView ID="GridView1" runat="server"
OnSorting="GridView1_Sorting"
AllowSorting="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#999999"
BorderStyle="Solid" BorderWidth="1px" CellPadding="3"
DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" >
<AlternatingRowStyle BackColor="#CCCCCC" />
警告:上面的代码根本没有经过测试,但我认为它应该做你想要的。
答案 4 :(得分:0)
我在另一个论坛上找到了一个解决方案,但它仍然存在缺陷 - 我想要的是默认的第一次点击排序顺序要降序,而不是升序。但是当我再次单击另一列时,它会将排序方向设置为再次升序。无论哪一列,它在每次点击时都会在降序和升序之间切换。现在我真正想要的是第一次点击任何列下降,如果我第二次点击任何列,它应该是升序。示例:我有2列,一个是薪水,另一个是年龄。现在我点击工资,第一个排序方向是降序,而不是默认升序(这就是代码的作用)。现在点击年龄,切换排序方向升序,我想要当我换到另一列时,它保持不变,但是我应该再次点击工资,它应该切换到升序(导致降序是第一次点击)。有什么建议吗?
守则: `
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null) //if viewstate doesn't contain any value, assume SortDirection.Ascending(cause thats the asp default)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set
{
ViewState["sortDirection"] = value; //Viewstate sort direction is set as a variable, so can contain either asc or desc
}
}
protected void GridView1_OnSorting(object sender, GridViewSortEventArgs e)
{
if (ViewState["sortDirection"] == null)
{
e.SortDirection = SortDirection.Descending;
}
else if (GridViewSortDirection == SortDirection.Ascending)
{
e.SortDirection = SortDirection.Descending;
}
else if (GridViewSortDirection == SortDirection.Descending)
{
e.SortDirection = SortDirection.Ascending;
}
GridViewSortDirection = e.SortDirection;
}
`
答案 5 :(得分:0)
我正在谷歌上搜索,因为我有同样的问题并在玩耍,发现如果我添加&#34; ORDER BY [Field Name]&#34;在我的SQL语句和启用排序中,每当我点击标题时,它都会颠倒任何列的顺序!我看到这是一个非常古老的线索,但也许它将来也会对其他人有所帮助。作为更深入的答案,这是我的数据源代码:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/webvideos.mdb"
DeleteCommand="DELETE FROM [Docs] WHERE [ID] = ?"
InsertCommand="INSERT INTO [Docs] ([ID], [Filename], [Label], [Section], [Index]) VALUES (?, ?, ?, ?, ?)"
SelectCommand="SELECT * FROM [Docs] ORDER BY ID"
UpdateCommand="UPDATE [Docs] SET [Filename] = ?, [Label] = ?, [Section] = ?, [Index] = ? WHERE [ID] = ?">
这是我的网格视图:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource1" AllowPaging="True"
PageSize="20" AllowSorting="True">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Label" HeaderText="Label" SortExpression="Label" />
<asp:BoundField DataField="Filename" HeaderText="Filename"
SortExpression="Filename" />
<asp:BoundField DataField="Section" HeaderText="Section"
SortExpression="Section" />
<asp:BoundField DataField="Index" HeaderText="Index" SortExpression="Index" />
</Columns>
</asp:GridView>