System.Web.UI.WebControls Namespace DataGrid Class
Occurs when a column is sorted.
[ VB ]
Public Event SortCommand As DataGridSortCommandEventHandler
[ C# ]
public event DataGridSortCommandEventHandler SortCommand;
[ C++ ]
public: __event DataGridSortCommandEventHandler* SortCommand;
In [ JScript ], you can handle the events defined by a class, but you cannot define your own.
The SortCommand event is raised whenever any of the LinkButton controls used for sorting a DataGrid is clicked.
The method assigned to handle the event is passed an argument of type DataGridSortCommandEventArgs object containing data related to this event. The following DataGridSortCommandEventArgs properties provide information specific to this event.
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 designate a handler for the SortCommand event at design time. Note that the AllowSorting property must be set for sorting to be enabled.
<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 DataGridSortCommandEventArgs DataGridSortCommandEventHandler