GridViewSortEventArgs.SortDirection Propiedad

Definición

Obtiene o establece la dirección en que se va a ordenar el control GridView.

public:
 property System::Web::UI::WebControls::SortDirection SortDirection { System::Web::UI::WebControls::SortDirection get(); void set(System::Web::UI::WebControls::SortDirection value); };
public System.Web.UI.WebControls.SortDirection SortDirection { get; set; }
member this.SortDirection : System.Web.UI.WebControls.SortDirection with get, set
Public Property SortDirection As SortDirection

Valor de propiedad

Uno de los valores de SortDirection.

Ejemplos

En el ejemplo siguiente se muestra cómo usar la SortDirection propiedad para determinar la dirección en la que se va a ordenar un GridView control. Si el usuario intenta ordenar en orden descendente, se cancela la operación de ordenación.


<%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
  <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="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 xmlns="http://www.w3.org/1999/xhtml" >
  <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>

Comentarios

Cuando se genera el Sorting evento, puede usar la SortDirection propiedad para determinar el orden en el que el GridView control ordenará sus elementos (según la expresión de ordenación) cuando se realice la operación de ordenación. Dado que el Sorting evento se produce antes de la operación de ordenación, también puede usar la SortDirection propiedad para establecer mediante programación la dirección de ordenación.

Se aplica a

Consulte también