如何使用 GridView 和 ObjectDataSource 进行排序? [英] How to sort using GridView and ObjectDataSource?

查看:18
本文介绍了如何使用 GridView 和 ObjectDataSource 进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 ObjectDataSource 的 GridView,我希望能够对其进行排序.

I have a GridView with an ObjectDataSource and I want to be able to sort it.

分页工作正常,但是排序给了我一个例外:

Paging works correctly, however Sorting gives me an exception:

GridView gridView 触发了未处理的事件排序.

The GridView gridView fired event Sorting which wasn't handled.

如何在服务器端启用排序?

How do I enable sorting on the server side?

(即 gridView.EnableSortingAndPagingCallbacks 必须保持为 false)

(i.e. gridView.EnableSortingAndPagingCallbacks must remains false)

推荐答案

将 gridView.AllowSorting 属性设置为 true.从这里开始,如果您使用实现 IBindingList 的对象,网格应该允许您在回发时自动对数据进行排序.但是,由于情况很可能并非如此,您应该接受上面 TheTXI 的建议并自己处理排序事件.在代码隐藏中连接 GridView.Sorting 事件,如下所示:

Set the gridView.AllowSorting property to true. From here the grid should allow you to sort data automatically on postback if you are using an object that implements IBindingList. However, since that is most likely not the case, you should take TheTXI's advice above and handle the sorting event yourself. Either wire the GridView.Sorting event in the codebehind, like so:

gridView.Sorting += new GridViewSortEventHandler(gridView_Sorting); 

处理 gridView_Sorting 方法内部的排序,应该是这样的:

Handle the sorting inside the gridView_Sorting method, which should look like this:

private void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
     //Sorting logic here
}

此外,您可以使用附加到控件的 OnSort="gridView_Sorting" 在页面本身上连接事件.

Also, you can wire the event on the page itself using OnSort="gridView_Sorting" attached to the control.

请记住,由于您将 gridView.EnableSortingAndPagingCallbacks 设置为 false,因此在用户尝试排序时不会立即触发此操作,而是会等待回发到服务器.

Remember, since you are setting gridView.EnableSortingAndPagingCallbacks to false, this will not be immediately fired when the user tries to sort, it instead will wait for the postback to the server.

我希望这会有所帮助!

由于 ObjectDataSource 似乎是首选的中间人,这里是对用于排序的接线的简要说明.在您的页面中使用以下内容(完整示例可以在 此处,靠近底部):

Since ObjectDataSource seems to be the middleman of choice, here is a brief explanation of wiring that for sorting as well. Use the following in your page (The full example can be found here on the MSDN, near the bottom):

<asp:GridView ID="TestGridView" runat="server" DataSourceID="ObjectDataSourceTest"
        AllowSorting="True">
    </asp:GridView>
    <asp:ObjectDataSource ID="ObjectDataSourceTest" runat="server" 
        SelectMethod="SelectMethod" 
        TypeName="Samples.AspNet.CS.SortingData" 
        SortParameterName="sortExpression">
    </asp:ObjectDataSource>

您将跳转到 ObjectDataSource 来处理排序,而不是实际使用 gridView.Sorting 事件.一旦触发排序,它应该调用在您的代码后面的 SelectMethod 中找到的方法.然后,在 SelectMethod 中,您将处理 GridView 对象的重建,它看起来像:

Instead of actually using the gridView.Sorting event, you'll be jumping over to the ObjectDataSource to take care of the sorting. Once the sort is triggered it should call the method found in SelectMethod in your code behind. Then, inside SelectMethod, you would handle the rebuilding of your GridView object, which would look like:

public void SelectMethod(string sortExpression)
{
     //Rebuild gridView table if necessary, same method used in 
     //on a postback, and retrieve data from the database. Once
     //completed sort the data with:

     gridView.Sort(sortExpression, SortDirection.(Ascending or Descending))
}

这篇关于如何使用 GridView 和 ObjectDataSource 进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆