System.Web.UI.WebControls Namespace DataGrid Class
Sets or retrieves a value specifying whether sorting is enabled.
Inline |
<asp:datagrid allowsorting = true | false ... > |
Script |
DataGrid.AllowSorting [ = true | false ] |
This property accepts or returns only a boolean value: true if sorting is enabled; otherwise, false. Default value is false.
Use the AllowSorting property to specify or determine whether sorting is enabled or disabled in a DataGrid control.
When sorting is enabled in a DataGrid using default bound columns, LinkButton controls are rendered in the header section of each column, enabling users to set the order of the data in the grid by the selected column.
When a LinkButton in the columns header is clicked, a SortCommand event occurs. It is up to you, though, to provide code to handle this event. The typical logic for the handler is to pass the field or expression to sort by, and then rebind the data to the DataGrid control.
The following code snippets demonstrate how to specify and code a handler for the SortCommand event to enable sorting in a DataGrid control.
The below code shows how to set the AllowSorting property and designate the handler for the SortCommand event at design time.
<asp:datagrid id = "myGrid" runat = "server"
...
allowsorting
onSortCommand = "sortGrid" />
The below shows the typical code for the SortCommand handler.
<script language = "C#" runat = "server">
string query = "SELECT CompanyName, ContactName, ... FROM Suppliers";
void Page_Load ( Object src, EventArgs e ) {
if ( !IsPostBack ) {
DataView myView = fetchData ( query, "northwind" ).Tables [ 0 ].DefaultView;
myView.Sort = "companyname";
myGrid.DataSource = myView;
myGrid.DataBind ( );
}
}
void sortGrid ( Object src, DataGridSortCommandEventArgs e ) {
DataView myView = fetchData ( query, "northwind" ).Tables [ 0 ].DefaultView;
myView.Sort = e.SortExpression;
myGrid.DataSource = myView;
myGrid.DataBind ( );
}
</script>
<script language = "VB" runat = "server">
Dim query As String = "SELECT CompanyName, ContactName, ... FROM Suppliers"
Sub Page_Load ( src As Object, e As EventArgs )
If Not IsPostBack Then
Dim myView As DataView = fetchData ( query, "northwind" ).Tables ( 0 ).DefaultView
myView.Sort = "companyname"
myGrid.DataSource = myView
myGrid.DataBind ( )
End If
End Sub
Sub sortGrid ( src As Object, e As DataGridSortCommandEventArgs )
Dim myView As DataView = fetchData ( query, "northwind" ).Tables ( 0 ).DefaultView
myView.Sort = e.SortExpression
myGrid.DataSource = myView
myGrid.DataBind ( )
End Sub
</script> |
|
C# |
VB |
Show me
DataGrid Members SortCommand