如何对 GridView 进行排序?
ASP.NET GridView 排序:我想在单击列名称时将排序方向从升序更改为降序。但默认情况下,排序方向始终显示升序。我是否缺少任何属性设置来切换排序方向?非常感谢任何帮助。谢谢。
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = myDataTable(strSQL);
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + SortDirection(e.SortDirection);
gridView.DataSource = dataView;
gridView.DataBind();
}
}
//e.SortDirection always showing ascending
用于排序的aspx gridview列设计:
<asp:BoundField DataField="CREATE_DATE" HeaderText="Create Date" SortExpression="CREATE_DATE" />
ASP.NET GridView
sorting: I want to change the sorting direction from ascending to descending when I click the column name. But by default, sorting direction always showing ascending. Am I missing any property setting to toggle sorting direction? Any help greatly appreciated. Thanks.
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = myDataTable(strSQL);
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + SortDirection(e.SortDirection);
gridView.DataSource = dataView;
gridView.DataBind();
}
}
//e.SortDirection always showing ascending
aspx gridview column design for sorting:
<asp:BoundField DataField="CREATE_DATE" HeaderText="Create Date" SortExpression="CREATE_DATE" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试设置
AllowSorting标记中
属性设置为 true?GridView
的或者,您可以在 DataBind 之后的代码中执行此操作:
此属性允许用户单击列标题以对该列进行排序。
编辑: 您实际上必须创建逻辑来决定是升序还是降序排序(
e.SortDirection
只会如果您使用“数据源控件”(例如SQLDataSource
),则会为您提供帮助。然后,您可以将 dataView.Sort 代码更改为如下所示:其中
someCondition
是决定哪种排序方式的逻辑(例如,您可以跟踪该列的最后一个排序方向) ,然后如果用户再次单击它,则执行相反的操作)。Did you try setting the
AllowSorting
attribute of theGridView
to true in the markup?Or, you could do it in your code behind after the DataBind:
This attribute allows the user to click on the column headers to sort on that column.
Edit: You actually have to create logic to decide whether to sort ascending or descending (
e.SortDirection
will only help you if your using a "data source control" likeSQLDataSource
). Then you can just change your dataView.Sort code to some like this:Where
someCondition
is your logic for deciding which way to sort (for instance you could keep track of what the last sort direction was for that column, then do the opposite if the user clicks it again).在绑定数据之前,请按照您想要的方向对其进行排序,然后执行以下操作:
如果您的数据源是 SQLDataSource 或类似的数据源,只需确保您的 select 语句按照您最初想要的方式进行排序即可。
Before you bind the data, sort it in the direction you want and then do:
If your datasource is
SQLDataSource
or something similar, just make sure your select statement does the sorting the way you want initially.