Server Side Pagination and Sorting In GridView Using ObjectDataSource In Asp.Net
On your Aspx page write down the code for Gridview as shown in the figure.
On GridView tag don't forget to add DataSourceID, AllowPaging, AllowSorting, PageSize and
AutoGenerateColumns="false".
On Aspx page, drag ObjectDataSource and add following things inside it.
ID -- It should be same as specified on the GridView.
SelectCountMethod - It is the Method Name for getting the total number of rows on the GridView.
SelectMethod - It is the Method Name that would bind the gridview with DataTable.
MaximumRowsParameterName - It is the page size that will be sent to
SelectMethod as pageSize from GridView.
SortParameterName - It is the Sort Column Name and Sort Direction that will be sent to
SelectMethod as sortBy from GridView.
StartRowIndexParameterName - It is the starting Index of GridView from where
we have to do the pagination.
For eg : If the PageSize is 10, then from Second page,
the StartRowIndexParameterName will be 11.
EnablePaging = "True"
Now on the Code behind page
1. Create one method named GetTotalCount for getting the total number of rows on the GridView.
2. Create one method named GetDataList for getting the data from db for binding the GridView.
3. Inputs for this GetDataList are startIndex, pageSize, sortBy which are
sent from the ObjectDataSource.
Among them startIndex and pageSize are already discussed.
sortBy contains Name and Direction of column that needs to be sorted.
If it is Ascending order then contains only column name
Else contains the Column Name+Space+DESC
4. By using the above parameters get the preferred data from database using LINQ or MSSQL.
5. The Result of this method is a DataTable that will be sent back to ObjectDataSource.