Anonymous
Anonymous

Reputation:

GridView sorting not working with ObjectDataSource

I use a GridView to bind using a ObjectDataSource using the DataSource property. Now, the problem is that I've a integer field which is shown like below:

<asp:GridView ... DataSource="MyObjectDataSource" OnSorting="MyGrdView_Sorting" >
<Columns>
<asp:BoundField DataField="IntegerField" Visible="False" SortExpression="IntegerField" />
</Columns>
</asp:GridView>

I also capture the RowCommand event for my business logic purpose and fire the Sort() method of GridView in there. In case, if I fire the Sort() method from Sorting event handler, I get stack overflow exception which I don't have clue why its happening?

Finally, even after doing the right things which I think are not happening here, sorting is just not working in my GridView with the IntegerField. What I'm doing wrong? :(

Upvotes: 1

Views: 2238

Answers (2)

sisve
sisve

Reputation: 19791

My first thought is that you're using the DataSource property when you probably should be using the DataSourceId property.

Edit:

Okay, I was too quick to answer, and too slow on the reading. ;) Could you share your implementation of MyGrdView_Sorting with us?

Second edit and answer to comment:

"I just do ((GridView)sender).Sort("IntegerField", SortDirection.Ascending); in the MyGrdView_Sorting handler."

Calling GridView.Sort will trigger the Sorting event, which in your case will call Sort, which will trigger the Sorting event, which will call Sort, ... do you see the pattern here? ;)

You will need to do the sorting at another location in your business logic, like you mention. You could set your ObjectDataSource.CanSort = true, specify a SortParameterName, and let your SelectMethod do the actual sorting.

Upvotes: 0

andleer
andleer

Reputation: 22578

Firing the Sort() on the Sorting event will fire the Sorting event again thus the stack overflow.

Upvotes: 1

Related Questions