Home > Data Access / ADO.NET > Data Source Controls Overview > Sorting Data with Data Source Controls
Web Forms Server Controls Controls You Can Use on Web Forms ASP.NET Data Source Controls
The GridView, FormView, and DetailsView controls can order the data that it is bound to by taking advantage of the sorting capabilities provided by data source controls.
The data source controls that support sorting are the SqlDataSource, AccessDataSource, and ObjectDataSource controls. The SqlDataSource and AccessDataSource controls support sorting only when their DataSourceMode property is set to DataSet ( the default ) as in the following example:
<asp:GridView id="empsGridView"
datasourceid="employees"
datakeynames="employeeid"
allowsorting
runat="server" />
<asp:sqldatasource id="employees"
selectcommand="SELECT employeeid, lastname, firstname FROM employees"
connectionstring="<%$ connectionStrings:northwindconnectionstring %>"
runat="server" />
The ObjectDataSource control supports sorting if the object returned by the SelectMethod is a DataSet, DataTable, or DataView object. The ObjectDataSource also supports retrieving results in sorted order from the data source.
When using the ObjectDataSource or SqlDataSource controls, you can take advantage of sorting capabilities using the SortParameterName property. You can set the SortParameterName property to the name of the parameter that contains a sort expression being passed to the data source control. The sort expression is a comma-delimited list of fields to sort by ( and optionally the DESC identifier to sort in descending order ). For details about the format of the sort expression, see the System.Data.DataView.Sort property.
The parameter identified by the SortParameterName property is passed to the ObjectDataSource control’s SelectMethod or passed as part of the parameter collection to the SqlDataSource control’s SelectCommand. The ObjectDataSource control can use the information passed to it in the sort parameter to return the data in sorted order. For the SqlDataSource control, you must supply the name of a stored procedure that can take the sort parameter and return the sorted data, because you cannot pass a parameter as part of an ORDER BY clause.
The following code example shows an ObjectDataSource control declaration that identifies a parameter named sortColumns
as the sort parameter name:
<asp:objectdatasource id="employeesobject" runat="server"
typename="Samples.AspNet.Controls.NorthwindEmployee"
sortparametername="sortcolumns"
enablepaging
startrowindexparametername="startrecord"
maximumrowsparametername="maxrecords"
selectmethod="GetAllEmployees" />
Binding to Data Using a Data Source Control Using Parameters with Data Source Controls