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

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

问题描述

我有一个ObjectDataSource的GridView,我希望能对它进行排序。

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

分页工作正常,但是排序给我一个例外:
GridView控件gridview的解雇事件排序这是不处理的。

Paging works correctly, however Sorting gives me an exception: "The GridView 'gridView' fired event Sorting which wasn't handled."

如何启用排序服务器端?

How do I enable sorting on the server side?

(即,gridView.EnableSortingAndPagingCallbacks必须保持假)

(i.e., gridView.EnableSortingAndPagingCallbacks must remain "false")

推荐答案

设置gridView.AllowSorting属性设置为true。从这里电网应该允许,如果您使用的是实现IBindingList的对象,你就自动回传的数据进行排序。但是,因为这是最有可能不是这样,应采取上​​述TheTXI的意见,并自己处理排序的事件。无论是丝GridView.Sorting事件在codebehind,像这样:

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似乎是选择的中间人,在这里是布线,对于分选以及的简要说明。请使用本网页(完整的例子可以发现以下<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.sortparametername.aspx\">here在MSDN上,在底部附近):

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>

而不是实际使用gridView.Sorting的事件,你会跳过到ObjectDataSource采取排序的照顾。一旦那种被触发,应后面调用SelectMethod发现该方法在您code。然后,里面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天全站免登陆