GridView.Sort(String, SortDirection) 方法

定义

根据指定的排序表达式和方向对 GridView 控件进行排序。

public:
 virtual void Sort(System::String ^ sortExpression, System::Web::UI::WebControls::SortDirection sortDirection);
public virtual void Sort (string sortExpression, System.Web.UI.WebControls.SortDirection sortDirection);
abstract member Sort : string * System.Web.UI.WebControls.SortDirection -> unit
override this.Sort : string * System.Web.UI.WebControls.SortDirection -> unit
Public Overridable Sub Sort (sortExpression As String, sortDirection As SortDirection)

参数

sortExpression
String

GridView 控件进行排序时使用的排序表达式。

sortDirection
SortDirection

SortDirection 值之一。

例外

GridView 控件被绑定到某个数据源控件,但是与该数据源关联的 DataSourceViewnull

示例

下面的示例演示如何使用 Sort 方法以编程方式按多个列对 GridView 控件进行排序。


<%@ 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 SortButton_Click(Object sender, EventArgs e)
  {

    String expression = "";
    SortDirection direction;

    // Create the sort expression from the values selected 
    // by the user from the DropDownList controls. Multiple
    // columns can be sorted by creating a sort expression
    // that contains a comma-separated list of field names.
    expression = SortList1.SelectedValue + "," + SortList2.SelectedValue;
    
    //  Determine the sort direction. The sort direction
    // applies only to the second column sorted.
    switch (DirectionList.SelectedValue)
    {
      case "Ascending":
        direction = SortDirection.Ascending;
        break;
      case "Descending":
        direction = SortDirection.Descending;
        break;
      default:
        direction = SortDirection.Ascending;
        break;
    }
    
    // Use the Sort method to programmatically sort the GridView
    // control using the sort expression and direction.
    CustomersGridView.Sort(expression, direction);
    
  }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView Sort Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView Sort Example</h3>

      <table>
        <tr>
          <td>
             Sort by:
            <asp:dropdownlist ID="SortList1"
              runat="server">
              <asp:listitem Selected="true">CustomerID</asp:listitem>
              <asp:listitem>CompanyName</asp:listitem>
              <asp:listitem>Address</asp:listitem>
              <asp:listitem>City</asp:listitem>
              <asp:listitem>PostalCode</asp:listitem>
              <asp:listitem>Country</asp:listitem>
            </asp:dropdownlist>
          </td>
          <td colspan="2">
             
          </td>
        </tr>
        <tr>
          <td>
            Then by:
              <asp:dropdownlist ID="SortList2"
                runat="server">
                <asp:listitem Selected="true">CustomerID</asp:listitem>
                <asp:listitem>CompanyName</asp:listitem>
                <asp:listitem>Address</asp:listitem>
                <asp:listitem>City</asp:listitem>
                <asp:listitem>PostalCode</asp:listitem>
                <asp:listitem>Country</asp:listitem>
              </asp:dropdownlist>
          </td>
          <td>
             Sort order:      
          </td>
          <td>
            <asp:radiobuttonlist id="DirectionList"
              runat="server">
              <asp:listitem selected="true">Ascending</asp:listitem>
              <asp:listitem>Descending</asp:listitem>
            </asp:radiobuttonlist>
          </td>
        </tr>
      </table>

      <asp:button id="SortButton"
        text="Sort"
        onclick="SortButton_Click" 
        runat="Server"/>  

      <br/>
      <hr/>
      <br/>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        allowpaging="true" 
        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 SortButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    
    Dim expression As String = ""
    Dim direction As SortDirection

    ' Create the sort expression from the values selected 
    ' by the user from the DropDownList controls. Multiple
    ' columns can be sorted by creating a sort expression
    ' that contains a comma-separated list of field names.
    expression = SortList1.SelectedValue & "," & SortList2.SelectedValue
    
    ' Determine the sort direction. The sort direction
    ' applies only to the second column sorted.
    Select Case DirectionList.SelectedValue
    
      Case "Ascending"
        direction = SortDirection.Ascending
      Case "Descending"
        direction = SortDirection.Descending
      Case Else
        direction = SortDirection.Ascending

    End Select
    
    ' Use the Sort method to programmatically sort the GridView
    ' control using the sort expression and direction.
    CustomersGridView.Sort(expression, direction)
    
  End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView Sort Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>GridView Sort Example</h3>

      <table>
        <tr>
          <td>
             Sort by:
            <asp:dropdownlist ID="SortList1"
              runat="server">
              <asp:listitem Selected="true">CustomerID</asp:listitem>
              <asp:listitem>CompanyName</asp:listitem>
              <asp:listitem>Address</asp:listitem>
              <asp:listitem>City</asp:listitem>
              <asp:listitem>PostalCode</asp:listitem>
              <asp:listitem>Country</asp:listitem>
            </asp:dropdownlist>
          </td>
          <td colspan="2">
             
          </td>
        </tr>
        <tr>
          <td>
            Then by:
              <asp:dropdownlist ID="SortList2"
                runat="server">
                <asp:listitem Selected="true">CustomerID</asp:listitem>
                <asp:listitem>CompanyName</asp:listitem>
                <asp:listitem>Address</asp:listitem>
                <asp:listitem>City</asp:listitem>
                <asp:listitem>PostalCode</asp:listitem>
                <asp:listitem>Country</asp:listitem>
              </asp:dropdownlist>
          </td>
          <td>
             Sort order:      
          </td>
          <td>
            <asp:radiobuttonlist id="DirectionList"
              runat="server">
              <asp:listitem selected="true">Ascending</asp:listitem>
              <asp:listitem>Descending</asp:listitem>
            </asp:radiobuttonlist>
          </td>
        </tr>
      </table>

      <asp:button id="SortButton"
        text="Sort"
        onclick="SortButton_Click" 
        runat="Server"/>  

      <br/>
      <hr/>
      <br/>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        allowpaging="true" 
        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>

注解

Sort使用 方法,使用指定的排序GridView表达式和方向以编程方式对控件进行排序。 排序表达式指定要用于排序的列。 若要对多个列进行排序,请创建包含以逗号分隔的字段名称列表的排序表达式。 排序方向指示是按升序还是降序执行排序。 当需要从控件外部(例如从页面上的其他控件)对控件进行排序 GridView 时,通常使用此方法。 此方法还通常用于在首次呈现控件时以编程方式设置 GridView 控件的默认排序顺序。 调用此方法还会引发 SortedSorting 事件。

适用于

另请参阅