Introduction
As you may know the GridView have built in support of sorting. In the current version of ASP.Net 1.x, we do not have the GridView control. Instead you have the DataGrid control. To handle the sorting of the DataGrid control you Have to build your oow sorting logic. this is not required with the new gridview control.
To enable the built in sort functionality of the GridView control, you set the AllowSorting property of the GridView to true. When the AllowSorting property is set to true the GrdiView will add a LinkButton for each column header and set the SortExpression to the filed bind to each column. for example, if the one column is bind to a filed, eg CustomerName the SortExpression property will be CustomerName. Setting the grid's AllowSorting property allows you to sort columns by default. you can disable sorting for individual fields (BoundColumn or TemplateColumns fields ) by Setting The Sortexpression Property of The Individual Column To an Empty String.
The sorting is handled by the data source control associated to the GridView control. Not all data source controls can sort data. The XmlDataSource can not sort any data. If the data source control associated to a GridView control supports sorting, the DataView object exposed By THE GRIDVIEW Control To Be Able To Display Sorted Data, The Data Source Control to Which It Is Bound Must Meet Certain Criteria:
The SqlDataSource Control Can Sort if ITS DatasourceMode Property is set to dataset, or its sortparameters, or it ...................
The ObjectDataSource Control Can Sortiff Retus A DataSet.The XmlDataSource Control Does Not Support Sorting.
The DataSetDataSource Control Supports sorting.
The AccessDataSource Control Can Sort.
How the sorting works
When you press the LinkButton for the column you want to be sorted, the Sorting event will be raised. The GridView passes the sort expression to the data source control and the data source control executes its SelectCommand with the sort expression as an argument. When this IS DONE, The Gridview Will Rebind It Self.
If the SqlDataSource control is used and the DataSourceMode property is set to DataSet, the sort expression will be set to the DataView's Sort property of the first table in the returned DataSet If the DataSourceMode is set to DataReader, the SortExpression will not work;. If you use it an exception will be thrown. to sort a DataReder the SortParameterName must be used. If you specify a value to the SortParameterName, you must use a stored procedure. The stored procedure must have an input parameter with the same name as the value specified in the SortParameterName property. If you set the SortParameterName to "sort" you must have a "@sort" input parameter added to the stored procedure. The SqlDataSource will pass the sort expression to the stored procedure's parameter.
If an ObjectDataSource is used and the SelectCommand method returns a DataSet the sort expression will be set to the DataView Sort property of the first table in the DataSet. If the SortParameterName is specified for the ObjectDataSource, the sort expression will be passed to the argument of The selectcommand method with the sortparametername.the datasetdatasource will set the sort expression to the dataview Sort Property for the first Table in the dataset.
..........
By default the sorting of a column in the GridView control will only sort on one column. To sort on several columns you can use the Sorting event of the GridView to change the sort expression. The following example uses a SqlDataSource control with its DataSourceMode is set to DataSet (This is the default mode of the SqlDataSource control), when one of the LinkButtons in the header of the GridView is pressed, the Sorting event will be executed. If the CompanyName column LinkButton is pressed, the sort expression will be changed and An Ascending Sort on Both CompanyName and The ContactName Columns Will Be Made:
<% @ Page language = "c #" classname = "default_aspx"%>