[英]sort columns of gridview in asp.net c#
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. gridview的数据绑定来自使用linq创建的datacontext。 I wanted to click the header of the column to sort the data.
我想单击列的标题来对数据进行排序。
Thanks! 谢谢!
There are 2 things you need to do to get this right. 要做到这一点,您需要做两件事。
Manually handle the Sorting event 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: 查看我的博客文章 ,了解如何执行此操作:
有关gridview中排序的更多信息可以在这里找到: MSDN Gridview排序用于获取数据的方法无关紧要,可以使用相同的排序。
add: 加:
AllowSorting="true"
to the <asp:GridView />
tag, that should do it 到
<asp:GridView />
标签,应该这样做
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. 也就是说,我没有在GridView代码中看到任何对数据源的引用。 You'll need something like this:
你需要这样的东西:
<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" ... />
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.
在此函数中编写代码以填充Gridview。 The only difference is to change the query based on GridViewSortEventArgs e
唯一的区别是基于GridViewSortEventArgs e更改查询
e.SortExpression and e.SortExpression和
e.SortDirection allways Ascending :-( e.SortDirection总是升序:-(
I hope this very short Answer helps 我希望这个非常简短的答案有帮助
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.