bingles
bingles

Reputation: 12223

ASP.NET GridView SqlDatasource Programmatic Databinding Sorting

I am databinding a SqlDatasource to a GridView in 2 different ways.

1) Declaring the SqlDatasource in the .aspx page and setting the DatasourceID property on the GridView to its id

2) Creating the SqlDatasource in the code behind and binding to the datasource property

When I use the declarative method (1), sorting works fine without any extra work on my part, but If I create the SqlDatasource in the code behind and try to sort, I get an exception saying 'The GridView 'myGridView' fired event Sorting which wasn't handled'.

I assume that there is some sort of extra plumbing that occurs when using the first method. Does anyone know what the datasourceid method does that the programmatic one doesn't? I would like to duplicate the logic in the code behind if possible.

Upvotes: 3

Views: 1495

Answers (1)

treaschf
treaschf

Reputation: 5944

The GridView checks whether it is data bound using the DataSourceId property, and if it's not, then the Sorting event must be handled to do the sorting.

Did you try adding the SqlDatasource which you have created programatically to the control hierarchy of the page (for example into a PlaceHolder), and then binding the GridView to it by using the DataSourceId property? I did not try this, but it should work.

Upvotes: 1

Related Questions