如何使用GridView和ObjectDataSource进行排序?

发布于 2024-07-24 07:00:51 字数 270 浏览 0 评论 0原文

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

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

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

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

(即 gridView.EnableSortingAndPagingCallbacks 必须保持 false)

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:

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

How do I enable sorting on the server side?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

驱逐舰岛风号 2024-07-31 07:00:51

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

gridView.Sorting += new GridViewSortEventHandler(gridView_Sorting); 

在 gridView_Sorting 方法内处理排序,该方法应如下所示:

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

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

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

我希望这有帮助!

编辑:

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

<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 对象的重建,如下所示:

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))
}

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); 

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

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

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

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.

I hope this helps!

EDIT:

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>

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))
}
做个ˇ局外人 2024-07-31 07:00:51

我正在使用 Linq2Sql 和 ObjectDataSource,它可以很好地进行分页和排序。

我实现了一个类用作 ObjectDataSource。 它有一个 Select 和一个 Count 方法,调用我的业务层,该业务层使用 Linq2SQL 查询从数据库检索数据。 select 方法自动获取第一项索引、页面大小和排序表达式作为参数。

public List<EntityClass> Select(int startIndex, int pageSize, string sortBy) {}
public int Count() {}

在 ASPX 中,数据源的配置如下:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"  
     SelectMethod="Select" EnablePaging="true"
     StartRowIndexParameterName="startIndex" 
     MaximumRowsParameterName="pageSize"
     SortParameterName="sortBy" SelectCountMethod="Count" >   
</asp:ObjectDataSource>

Select 和 Count 方法使用 Linq 查询从 DB 检索数据。 我使用 Skip()、Take()Orderby() 方法。 为了让 OrderBy 接受字符串排序表达式,我使用 DynamicLinq 无需编写太多代码,数据绑定、分页和排序都会自动工作。

I am using Linq2Sql and a ObjectDataSource and it does Paging and Sorting very well.

I implemented a Class to be used as the ObjectDataSource. It has a Select and a Count method calling my business layer which uses Linq2SQL queries to retrieve data from the DB. The select methods gets the first item index, page size and the sort expression as parameters automatically.

public List<EntityClass> Select(int startIndex, int pageSize, string sortBy) {}
public int Count() {}

In the ASPX, the DataSource is configured like this:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"  
     SelectMethod="Select" EnablePaging="true"
     StartRowIndexParameterName="startIndex" 
     MaximumRowsParameterName="pageSize"
     SortParameterName="sortBy" SelectCountMethod="Count" >   
</asp:ObjectDataSource>

The Select and the Count method use Linq queries to retrieve the data from the DB. I use the Skip(), Take() and Orderby() methods. For the OrderBy to accept a string sort expression I use DynamicLinq There is not much to code, Databinding, paging and Sorting are automatically working.

何以心动 2024-07-31 07:00:51

在调用 StoredProcedure 的数据访问层方法中传递 SortExpression
并编写下面的 SP 方式来处理 SQL 上的排序。 这样您就可以提高排序的性能。

数据库SP:

Select ROW_NUMBER() OVER(ORDER BY   '+@sortExpression +' ) as RowNum
,* from (SELECT CUSTOMERID,  
LEDGERDESCRIPTION,  
CustomerDescription as CustomerName
WHERE REGIONID ='''+@RegionID+''')t
order by RowNum'

Pass SortExpression in method of Data Access Layer that calls StoredProcedure
and write below way SP to handle sorting on SQL. This way you can improve the performance of your sorting.

Database SP:

Select ROW_NUMBER() OVER(ORDER BY   '+@sortExpression +' ) as RowNum
,* from (SELECT CUSTOMERID,  
LEDGERDESCRIPTION,  
CustomerDescription as CustomerName
WHERE REGIONID ='''+@RegionID+''')t
order by RowNum'
情愿 2024-07-31 07:00:51

为此,您可以使用 LINQ,只需在所选列上使用 OrderBy 即可:

    public static List<YourDataObject> GetSortedData(string orderBy)
    {
        List<YourDataObject> sortedDataList = new List<YourDataObject>();

        switch (orderBy)
        {
            case "Col1": sortedEmployeeList = GetDefaultObjects().OrderBy(x => x.Col1).ToList();
                break;
            case "Col2": 
                         //Do this for all columns
            default:
                sortedEmployeeList = GetDefaultObjects();
                break;
        }

        return sortedEmployeeList;
    }

这是在对象数据源的 Select 方法中完成的。

在 GridView 中添加排序键。 并在对象数据源中添加 SelectParameter 一个 `orderBy'

<asp:GridView AllowSorting="true" DataSourceID="objDsAllObjects" ....>
<Columns>
         <asp:BoundField SortExpression="Col1"/>
         <!-- Do this for all columns -->
</Columns>
</asp:GridView >

<asp:ObjectDataSource ID="objDsAllObjects" SortParameterName="orderBy" runat="server" 
SelectMethod="GetAllEmployees" TypeName="YourObjectClass"></asp:ObjectDataSource>

它对我有用,而无需更改 DAL 层和存储过程。

You can use LINQ for this just use OrderBy on the selected column, do this:

    public static List<YourDataObject> GetSortedData(string orderBy)
    {
        List<YourDataObject> sortedDataList = new List<YourDataObject>();

        switch (orderBy)
        {
            case "Col1": sortedEmployeeList = GetDefaultObjects().OrderBy(x => x.Col1).ToList();
                break;
            case "Col2": 
                         //Do this for all columns
            default:
                sortedEmployeeList = GetDefaultObjects();
                break;
        }

        return sortedEmployeeList;
    }

This is done in the Select method of the object data source.

In the GridView add sort keys in it. And add SelectParameter in the object data source a `orderBy'

<asp:GridView AllowSorting="true" DataSourceID="objDsAllObjects" ....>
<Columns>
         <asp:BoundField SortExpression="Col1"/>
         <!-- Do this for all columns -->
</Columns>
</asp:GridView >

<asp:ObjectDataSource ID="objDsAllObjects" SortParameterName="orderBy" runat="server" 
SelectMethod="GetAllEmployees" TypeName="YourObjectClass"></asp:ObjectDataSource>

It worked for me without changing the DAL layer and stored procedures.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文