英語で読む

次の方法で共有


GridViewSortEventArgs クラス

定義

Sorting イベントのデータを提供します。

C#
public class GridViewSortEventArgs : System.ComponentModel.CancelEventArgs
継承
GridViewSortEventArgs

次の例では、イベント処理メソッドに渡されたオブジェクトを使用 GridViewSortEventArgs して、ユーザーがコントロールのアドレス列を並べ替えようとしたときに並べ替え操作を取り消す方法を GridView 示します。

ASP.NET (C#)

<%@ 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)
  {
    // Cancel the sorting operation if the user attempts
    // to sort by address.
    if (e.SortExpression == "Address")
    {
      e.Cancel = true;
      Message.Text = "You cannot sort by address.";
      SortInformationLabel.Text = "";
    }
    else
    {
      Message.Text = "";
    }
  }

  void CustomersGridView_Sorted(Object sender, EventArgs e)
  {
    // Display the sort expression and sort direction.
    SortInformationLabel.Text = "Sorting by " +
      CustomersGridView.SortExpression.ToString() +
      " in " + CustomersGridView.SortDirection.ToString() +
      " order.";
  }
  
</script>

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

      <asp:label id="Message"
        forecolor="Red"
        runat="server"/>
        
      <br/>
        
      <asp:label id="SortInformationLabel"
        forecolor="Navy"
        runat="server"/>
                
      <br/>  

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

注釈

コントロールはGridView、並べ替えボタン (プロパティが "Sort" に設定されたボタンCommandName) がクリックされたときに、コントロールが並べ替え操作をGridView処理する前にイベントを発生Sortingさせます。 これにより、このイベントが発生するたびに、並べ替え操作のキャンセルなどのカスタム ルーチンを実行するイベント処理メソッドを提供できます。

注意

プロパティが "Sort" に設定されているコントロールCommandNameGridViewのボタンはイベントをSorting発生させますが、通常、並べ替えボタンはコントロールのGridViewヘッダー行に表示されます。

GridViewSortEventArgsオブジェクトはイベント処理メソッドに渡されます。これにより、コントロールを並べ替える並べ替え式と並べ替え方向をGridView指定または決定できます。 並べ替え式を確認するには、 プロパティを使用します SortExpression 。 並べ替えの方向を確認するには、 プロパティを使用します SortDirection 。 プロパティを にtrue設定して並べ替え操作をCancel取り消すこともできます。

イベントを処理する方法の詳細については、次を参照してください。処理とイベントの発生します。

のインスタンスの初期プロパティ値の GridViewSortEventArgs一覧については、 コンストラクターを GridViewSelectEventArgs 参照してください。

コンストラクター

GridViewSortEventArgs(String, SortDirection)

GridViewSortEventArgs クラスの新しいインスタンスを初期化します。

プロパティ

Cancel

イベントをキャンセルするかどうかを示す値を取得または設定します。

(継承元 CancelEventArgs)
SortDirection

GridView コントロールを並べ替える方向を取得または設定します。

SortExpression

GridView コントロール内の項目の並べ替えに使用する式を取得または設定します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

製品 バージョン
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

こちらもご覧ください