views:

3055

answers:

4

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 remain "false")

A: 

Gridview.Sorting Event should give you plenty of help and examples on how to work through this.

TheTXI
+6  A: 

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))
}
Zensar
Wow, overly critical, I'll elaborate and edit my post.
Zensar
So, how do you tell the ObjectDataSource to sort? I do have AllowSorting set to true, and my data source implements Select(string sortExpression, int maximumRows, int startRowIndex).Maybe it is working, but I'm just not getting a postback to the server when a column header is clicked?I'm expecting the ObjectDataSource calls Select with the sort parameter when I click a header, but it doesn't seem to be the case. It does, however, work when EnableSortingAndPagingCallbacks is set, but I can't use TemplateFields then.
Jonathan Mitchem
EnableSortingAndPagingCallbacks is the property that allows the page to do a CLIENT SIDE sorting of the data through Java. When EnableSortingAndPagingCallbacks is set to false, then the sorting events will only be run when a postback is fired (ie... clicking a button, etc...). If AllowSorting has been enabled, then the header should automatically become a Link type button, which will cause a postback and fire the gridView.Sorting event. I would do a quick debug to make sure the event is firing first, then move on from there.
Zensar
Yes, the event is firing. However, I have not been able to find any examples of using an ObjectDataSource for sorting within a GridView_Sorting implementation. Normally, ObjectDataSorce appears to "automatically" do the sort, by calling the Select method on the associated Type, with the appropriate parameter corresponding to the SortExpression of the column, however the call itself is all behind the scenes. I have no problem making such piping explicit and implementing it, however I do not know how to do it. Have you worked with sorting with an ObjectDataSource?
Jonathan Mitchem
I will edit my answer and expand.
Zensar
Yes, that is exactly what I'm doing, but it's not working if I set EnableSortingAndPagingCallbacks to false. Hence, the question. I suppose http://stackoverflow.com/questions/1002196/how-to-sort-on-a-gridview-using-objectdatasource-with-templatefields elaborates what I'm doing better.
Jonathan Mitchem
A: 

Thanks.. Bind Object Datasource to gridview. Object Datasource select method Return Dataset..So how to sorting in it.

Smita
A: 

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.

PeterTheNiceGuy