在asp.net c#中对gridview的列进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/213148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sort columns of gridview in asp.net c#
提问by
Can anyone tell the function to sort the columns of a gridview in c# asp.net.
谁能告诉函数在 c# asp.net 中对 gridview 的列进行排序。
The databound to gridview is from datacontext created using linq. I wanted to click the header of the column to sort the data.
绑定到 gridview 的数据来自使用 linq 创建的数据上下文。我想单击列的标题来对数据进行排序。
Thanks!
谢谢!
回答by Kon
回答by Jeremy B.
more information on sorting in a gridview can be found here: MSDN Gridview sortingthe methodology used to get the data should not matter, you can use the same sorting.
有关在 gridview 中排序的更多信息可以在这里找到:MSDN Gridview 排序用于获取数据的方法应该无关紧要,您可以使用相同的排序。
回答by craigmoliver
add:
添加:
AllowSorting="true"
to the <asp:GridView />
tag, that should do it
到<asp:GridView />
标签,应该这样做
回答by Daniel Schaffer
When I do that Alone it gives an error "The GridView 'GridView1' fired event Sorting which wasn't handled.
当我单独执行此操作时,它会出现错误“未处理的 GridView 'GridView1' 触发事件排序。
I've had that happen before... I've just created a throwaway handler, and then everything seemed to start working after that. Not the prettiest solution, but it worked for me.
我以前遇到过这种情况......我刚刚创建了一个一次性处理程序,然后一切似乎都开始工作了。不是最漂亮的解决方案,但它对我有用。
That said, I didn't see any reference to a data source in your GridView code. You'll need something like this:
也就是说,我在您的 GridView 代码中没有看到任何对数据源的引用。你需要这样的东西:
<asp:LinqDataSource ID="dsMyDataSource" runat="server"
DataContextTypeName="MyDataContext"
TableName="MyTable"
AllowSort="true" />
And then in your GridView:
然后在您的 GridView 中:
<asp:GridView ID="gvMyGridView" runat="server" DataSourceID="dsMyDataSource" ... />
回答by Georg
In the Properties Panel double Click on the Sorting Entry. A new Function will be created. In this Function write the Code to fill the Gridview. The only difference is to change the query based on GridViewSortEventArgs e
在属性面板中双击排序条目。将创建一个新函数。在这个函数中编写代码来填充 Gridview。唯一的区别是根据 GridViewSortEventArgs e 更改查询
e.SortExpression
and
e.SortDirection allways Ascending :-(
e.SortExpression 和
e.SortDirection 总是升序 :-(
I hope this very short Answer helps
我希望这个非常简短的答案有帮助
回答by Georg
In Half Pseudocode for SQL Query
SQL查询的半伪代码
string Query= string.Empty;
string SortExpression = string.Empty;
// HDFSort is an HiddenField !!!
protected void SortCommand_OnClick(object sender, GridViewSortEventArgs e)
{
SortExpression = e.SortExpression;
Query = YourQuery + " ORDER BY "+SortExpression +" "+ HDFSort.Value ;
HDFSort.Value = HDFSort.Value== "ASC" ? "DESC" : "ASC";
RefreshGridView();
}
protected void RefreshGridView()
{
GridView1.DataSource = DBObject.GetData(Query);
GridView1.DataBind();
}
回答by davidfowl
There are 2 things you need to do to get this right.
您需要做两件事才能做到这一点。
- Keep the sorting state is viewstate(SortDirection and SortExpression)
- You generate the correct linq expression based on the current sorting state.
- 保持排序状态为viewstate(SortDirection and SortExpression)
- 您可以根据当前的排序状态生成正确的 linq 表达式。
Manually handle the Sortingevent in the grid and use this helper I wrote to sort by SortExpression and SortDirection:
手动处理网格中的Sorting事件,并使用我编写的这个助手按 SortExpression 和 SortDirection 进行排序:
public static IQueryable<T> SortBy<T>(IQueryable<T> source, string sortExpression, SortDirection direction) {
if (source == null) {
throw new ArgumentNullException("source");
}
string methodName = "OrderBy";
if (direction == SortDirection.Descending) {
methodName += "Descending";
}
var paramExp = Expression.Parameter(typeof(T), String.Empty);
var propExp = Expression.PropertyOrField(paramExp, sortExpression);
// p => p.sortExpression
var sortLambda = Expression.Lambda(propExp, paramExp);
var methodCallExp = Expression.Call(
typeof(Queryable),
methodName,
new[] { typeof(T), propExp.Type },
source.Expression,
Expression.Quote(sortLambda)
);
return (IQueryable<T>)source.Provider.CreateQuery(methodCallExp);
}
db.Products.SortBy(e.SortExpression, e.SortDirection)
db.Products.SortBy(e.SortExpression, e.SortDirection)
Check out my blog post on how to do this:
查看我的博客文章,了解如何执行此操作: