Updated: August 2008
Gets or sets the direction in which to sort the GridView control.
Namespace:
System.Web.UI.WebControls
Assembly:
System.Web (in System.Web.dll)
Visual Basic (Declaration)
Public Property SortDirection As SortDirection
Dim instance As GridViewSortEventArgs
Dim value As SortDirection
value = instance.SortDirection
instance.SortDirection = value
public SortDirection SortDirection { get; set; }
public:
property SortDirection SortDirection {
SortDirection get ();
void set (SortDirection value);
}
public function get SortDirection () : SortDirection
public function set SortDirection (value : SortDirection)
When the Sorting event is raised, you can use the SortDirection property to determine the order in which the GridView control will sort its items (based on the sort expression) when the sort operation is performed. Because the Sorting event occurs before the sort operation, you can also use the SortDirection property to programmatically set the sort direction.
Note: |
|---|
If you bind the GridView control to a data source by programmatically setting the DataSource property, the SortDirection property always returns SortDirection..::.Ascending. In this case, you must provide the sorting functionality in the event-handler method for the Sorting event. |
The following example demonstrates how to use the SortDirection property to determine the direction in which to sort a GridView control. If the user attempts to sort in descending order, the sort operation is canceled.
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub CustomersGridView_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
' By default, the sort order toggles when the user clicks
' the same sort button repeatedly. For this example, cancel
' the sort operation if the user attempts to sort in descending
' order.
If e.SortDirection = SortDirection.Descending Then
e.Cancel = True
Message.Text = "Sorting in descending order is not supported."
Else
Message.Text = ""
End If
End Sub
</script>
<html >
<head runat="server">
<title>GridViewSortEventArgs SortDirection Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridViewSortEventArgs SortDirection Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
allowpaging="true"
emptydatatext="No data available."
allowsorting="true"
onsorting="CustomersGridView_Sorting"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void CustomersGridView_Sorting(Object sender, GridViewSortEventArgs e)
{
// By default, the sort order toggles when the user clicks
// the same sort button repeatedly. For this example, cancel
// the sort operation if the user attempts to sort in descending
// order.
if (e.SortDirection == SortDirection.Descending)
{
e.Cancel = true;
Message.Text = "Sorting in descending order is not supported.";
}
else
{
Message.Text = "";
}
}
</script>
<html >
<head runat="server">
<title>GridViewSortEventArgs SortDirection Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridViewSortEventArgs SortDirection Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
allowpaging="true"
emptydatatext="No data available."
allowsorting="true"
onsorting="CustomersGridView_Sorting"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0
Reference
Date | History | Reason |
|---|
August 2008
| Added a note to the remarks. |
Customer feedback.
|