ASp.NET 2.0 GridView, ObjectDataSource and sorting

Posts   
 
    
Kenneth avatar
Kenneth
User
Posts: 10
Joined: 08-Jul-2004
# Posted on: 15-Aug-2005 23:40:20   

Consider this code:

aspx:


:
<asp:ObjectDataSource ID="LocationData" runat="server" SelectMethod="GetLocations" TypeName="Helper"></asp:ObjectDataSource>
    <asp:GridView ID="LocationGrid" runat="server" AllowPaging="True" AllowSorting="true" DataSourceID="LocationData" AutoGenerateColumns="false">
:

helper-class:


:
    public LocationCollection GetLocations()
    {
        CustomerEntity Cust = new CustomerEntity(GetCurrentCustomerId());
        
        return Cust.GetMultiLocation(false);
    }
:

For some reason the ObjectDataSource only support sorting if the SortParameterName is set or if the method returns a DataSet.

Suggestions?

Thanks in advance!

Kenneth Solberg

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 16-Aug-2005 11:54:26   

Sorting in the collection is disabled by default. Instead of:


return Cust.GetMultiLocation(false);

do:


LocationCollection toReturn = Cust.GetMultiLocation(false);
toReturn.SupportsSorting=true;
return toReturn;

Frans Bouma | Lead developer LLBLGen Pro
Kenneth avatar
Kenneth
User
Posts: 10
Joined: 08-Jul-2004
# Posted on: 16-Aug-2005 13:46:02   

Excellent sunglasses

Edit: although the grid doesn't sort ...



    <asp:GridView ID="LocationGrid" runat="server" DataKeyNames="LocationId" AllowSorting="True" DataSourceID="LocationData" AutoGenerateColumns="false">
        <Columns>       
            
            <asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name" /> 



Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 16-Aug-2005 15:27:25   

Hmm. It should, it's normal IBindingList code, the same DataView objects use... Do you need to enable the grid for sorting as well (I have no idea, haven't used asp.net 2.0 yet) ?

Frans Bouma | Lead developer LLBLGen Pro
TogasPoon
User
Posts: 42
Joined: 09-Feb-2006
# Posted on: 13-Feb-2006 23:56:29   

Any word on this, I'm currently evaluating your product and taking baby steps. When i try and sort my gridview (after setting SupportsSorting = True) I get this error.

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

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowSorting="True" DataKeyNames="EmpId">
            <Columns>  
                <asp:BoundField DataField="EmpId" HeaderText="ID" SortExpression="EmpId" />
                <asp:BoundField DataField="FirstName" HeaderText="Name" SortExpression="FirstName" />
            </Columns>
        </asp:GridView>

and...

        Dim myReps As New EmployeeCollection
        myReps.GetMulti(Nothing)

        myReps.SupportsSorting = True
        Me.GridView1.DataSource = myReps
        Me.DataBind()
bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 14-Feb-2006 03:55:02   

I believe you would want to set up an event handler for the sorting event. You can use the GridViewSortEventArgs to determine which column was selected and then rebind the datasource using the column to create a sort expression for the getmulti().