クリックして評価とフィードバックをお寄せください
MSDN
MSDN ライブラリ
.NET 開発
以前のバージョン
.NET Framework SDK 2.0
System.Web.UI.WebControls
GridView クラス
GridView イベント
 RowCommand イベント
このページは次のバージョンについて記述しています。
Microsoft Visual Studio 2005/.NET Framework 2.0

その他のバージョンについては、以下の情報を参照してください。
GridView.RowCommand イベント
GridView コントロールのボタンがクリックされたときに発生します。

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)

Visual Basic (宣言)
Public Event RowCommand As GridViewCommandEventHandler
Visual Basic (使用法)
Dim instance As GridView
Dim handler As GridViewCommandEventHandler

AddHandler instance.RowCommand, handler
C#
public event GridViewCommandEventHandler RowCommand
C++
public:
event GridViewCommandEventHandler^ RowCommand {
	void add (GridViewCommandEventHandler^ value);
	void remove (GridViewCommandEventHandler^ value);
}
J#
/** @event */
public void add_RowCommand (GridViewCommandEventHandler value)

/** @event */
public void remove_RowCommand (GridViewCommandEventHandler value)
JScript
JScript では、このクラスで定義されているイベントを処理できます。ただし、独自のイベントは定義できません。
XAML
適用できません。

RowCommand イベントは、GridView コントロールのボタンがクリックされたときに発生します。これにより、このイベントが発生するたびにカスタム ルーチンを実行するイベント処理メソッドを提供できます。

GridView コントロール内のボタンから、コントロールの組み込み機能の一部を呼び出すこともできます。これらのいずれかの操作を実行するには、ボタンの CommandName プロパティを次の表の値のいずれかに設定します。

CommandName 値

説明

"Cancel"

編集操作をキャンセルし、GridView コントロールを読み取り専用モードに戻します。RowCancelingEdit イベントを発生させます。

"Delete"

現在のレコードを削除します。RowDeleting イベントおよび RowDeleted イベントを発生させます。

"Edit"

現在のレコードを編集モードにします。RowEditing イベントを発生させます。

"Page"

ページング操作を実行します。ボタンの CommandArgument プロパティを "First"、"Last"、"Next"、"Prev"、またはページ番号に設定して、実行するページング操作の種類を指定します。PageIndexChanging イベントおよび PageIndexChanged イベントを発生させます。

"Select"

現在のレコードを選択します。SelectedIndexChanging イベントおよび SelectedIndexChanged イベントを発生させます。

"Sort"

GridView コントロールを並べ替えます。Sorting イベントおよび Sorted イベントを発生させます。

"Update"

データ ソースの現在のレコードを更新します。RowUpdating イベントおよび RowUpdated イベントを発生させます。

前述の表に示されているボタンがクリックされると RowCommand イベントが発生しますが、表に示されているイベントを使用して操作を行うことをお勧めします。

GridViewCommandEventArgs オブジェクトがイベント処理メソッドに渡されることにより、クリックされたボタンのコマンド名およびコマンド引数を確認できます。

メモメモ :

GridViewCommandEventArgs クラスには、どの行のボタンがクリックされたのかを示すプロパティはありません。イベントの発生元の行についての情報が必要な場合は、CommandArgument プロパティを使用して、行のインデックスをイベント処理メソッドに渡します。

イベント処理の詳細については、「イベントの利用」を参照してください。

RowCommand イベントを使用して、行の Add ボタンがクリックされたときに GridView コントロールから ListBox コントロールに顧客の名前を追加する方法を次のコード例に示します。

Visual Basic
<%@ 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_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

    ' If multiple buttons are used in a GridView control, use the
    ' CommandName property to determine which button was clicked.
    If e.CommandName = "Add" Then
    
      ' Convert the row index stored in the CommandArgument
      ' property to an Integer.
      Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            
      ' Retrieve the row that contains the button clicked 
      ' by the user from the Rows collection.
      Dim row As GridViewRow = CustomersGridView.Rows(index)
            
      ' Create a new ListItem object for the customer in the row.     
      Dim item As New ListItem()
      item.Text = Server.HtmlDecode(row.Cells(2).Text)
            
      ' If the customer is not already in the ListBox, add the ListItem 
      ' object to the Items collection of the ListBox control. 
      If Not CustomersListBox.Items.Contains(item) Then
      
        CustomersListBox.Items.Add(item)
        
      End If
      
    End If
    
  End Sub

  Sub CustomersGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    
    ' The GridViewCommandEventArgs class does not contain a 
    ' property that indicates which row's command button was
    ' clicked. To identify which row's button was clicked, use 
    ' the button's CommandArgument property by setting it to the 
    ' row's index.
    If e.Row.RowType = DataControlRowType.DataRow Then
    
      ' Retrieve the LinkButton control from the first column.
      Dim addButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)
          
      ' Set the LinkButton's CommandArgument property with the
      ' row's index.
      addButton.CommandArgument = e.Row.RowIndex.ToString()
      
    End If

  End Sub
    
</script>

<html  >
  <head runat="server">
    <title>GridView RowCommand Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView RowCommand Example</h3>
            
      <table width="100%">         
        <tr>                
          <td style="width:50%">
                    
            <asp:gridview id="CustomersGridView" 
              datasourceid="CustomersSource"
              allowpaging="true" 
              autogeneratecolumns="false"
              onrowcommand="CustomersGridView_RowCommand"
              onrowcreated="CustomersGridView_RowCreated"  
              runat="server">
                
              <columns>
                <asp:buttonfield buttontype="Link" 
                  commandname="Add" 
                  text="Add"/>
                <asp:boundfield datafield="CustomerID" 
                  headertext="Customer ID"/>
                <asp:boundfield datafield="CompanyName" 
                  headertext="Company Name"/> 
                <asp:boundfield datafield="City" 
                  headertext="City"/>         
              </columns>
                
            </asp:gridview>
                    
          </td>
                    
          <td style="vertical-align:top; width:50%">
                    
            Customers: <br/>
            <asp:listbox id="CustomersListBox"
              runat="server"/> 
                    
          </td>  
        </tr>      
      </table>
            
      <!-- 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], [City] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

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_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Add")
    {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);
            
      // Retrieve the row that contains the button clicked 
      // by the user from the Rows collection.
      GridViewRow row = CustomersGridView.Rows[index];
            
      // Create a new ListItem object for the customer in the row.     
      ListItem item = new ListItem();
      item.Text = Server.HtmlDecode(row.Cells[2].Text);
            
      // If the customer is not already in the ListBox, add the ListItem 
      // object to the Items collection of the ListBox control. 
      if (!CustomersListBox.Items.Contains(item))
      {
        CustomersListBox.Items.Add(item);
      }           
    }
  }

  void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
  {
    
    // The GridViewCommandEventArgs class does not contain a 
    // property that indicates which row's command button was
    // clicked. To identify which row's button was clicked, use 
    // the button's CommandArgument property by setting it to the 
    // row's index.
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Retrieve the LinkButton control from the first column.
      LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
          
      // Set the LinkButton's CommandArgument property with the
      // row's index.
      addButton.CommandArgument = e.Row.RowIndex.ToString();
    }

  }
    
</script>

<html  >
  <head runat="server">
    <title>GridView RowCommand Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView RowCommand Example</h3>
            
      <table width="100%">         
        <tr>                
          <td style="width:50%">
                    
            <asp:gridview id="CustomersGridView" 
              datasourceid="CustomersSource"
              allowpaging="true" 
              autogeneratecolumns="false"
              onrowcommand="CustomersGridView_RowCommand"
              onrowcreated="CustomersGridView_RowCreated"  
              runat="server">
                
              <columns>
                <asp:buttonfield buttontype="Link" 
                  commandname="Add" 
                  text="Add"/>
                <asp:boundfield datafield="CustomerID" 
                  headertext="Customer ID"/>
                <asp:boundfield datafield="CompanyName" 
                  headertext="Company Name"/> 
                <asp:boundfield datafield="City" 
                  headertext="City"/>         
              </columns>
                
            </asp:gridview>
                    
          </td>
                    
          <td style="vertical-align:top; width:50%">
                    
            Customers: <br/>
            <asp:listbox id="CustomersListBox"
              runat="server"/> 
                    
          </td>  
        </tr>      
      </table>
            
      <!-- 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], [City] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

.NET Framework

サポート対象 : 3.0,2.0
コミュニティ コンテンツ   コミュニティ コンテンツとは
新しいコンテンツの追加 RSS  注釈
Processing
Page view tracker