views:

1345

answers:

2

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? :(

A: 

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.

Simon Svensson
I just do ((GridView)sender).Sort("IntegerField", SortDirection.Ascending); in the MyGrdView_Sorting handler. However, by doing this, I get Stack Overflow exception, so, i do the same in my business logic now.
Hi, There is no such property named ObjectDataSource.CanSort. Firstly, CanSort is a property of ObjectDataSourceView and secondly, it is a read-only property which can't be set.However, I do understand why I get Stack overflow exception with the pattern explained. Thanks!
@Anonymous: True, I was too quick in reading the implementation of ExecuteSelect. The ObjectDataSourceView.CanSort property is hardcoded to return true, so specifying SortParameterName should be enough.
Simon Svensson
I tried by specifying SortParameterName in my ObjectDataSource definition. However, it do asks for an implementation of a Select with the SortParameter being passed as input parameter; but when I debug it then it never hits the new def rather it always go for Select with no params. Any Clues?
@Anonymous, I would need to see your code, preferably a repeatable test case, to give a good answer to that. My only thought would be to retry without overloads, but methods with unique names.
Simon Svensson
Well, I tried with your options by changing my business logic and not overriding functions. Also, this time provide sorting functionality from within the stored proc so as to be very sure that the data comes pre-sorted still my bad luck that the grid shows unsorted data.
@Anonymous, the grid will always show the data in the order it is provided. So the random sorting is applied either in your stored procedure (which I presume you have already checked) or in your business layer. Hard to debug without any code or test case.
Simon Svensson
+1  A: 

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

Andrew Robinson