GridViewCommandEventHandler デリゲートとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > GridViewCommandEventHandler デリゲートの意味・解説 

GridViewCommandEventHandler デリゲート

メモ : このデリゲートは、.NET Framework version 2.0新しく追加されたものです。

GridView コントロールの RowCommand イベント処理するメソッド表します

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

Public Delegate Sub GridViewCommandEventHandler
 ( _
    sender As Object, _
    e As GridViewCommandEventArgs _
)
Dim instance As New GridViewCommandEventHandler(AddressOf
 HandlerMethod)
public delegate void GridViewCommandEventHandler
 (
    Object sender,
    GridViewCommandEventArgs e
)
public delegate void GridViewCommandEventHandler
 (
    Object^ sender, 
    GridViewCommandEventArgs^ e
)
/** @delegate */
public delegate void GridViewCommandEventHandler
 (
    Object sender, 
    GridViewCommandEventArgs e
)
JScript では、デリゲート使用できますが、新規に宣言することはできません。

パラメータ

sender

イベントソース

e

イベント データ格納している GridViewCommandEventArgs オブジェクト

解説解説
使用例使用例

GridViewCommandEventHandler デリゲートを、プログラムによって GridView コントロールRowCommand イベント追加する方法次の例に示します

<%@ Page language="VB" %>

<script runat="server">

  Sub Page_Load(ByVal sender As
 Object, ByVal e As EventArgs)

    ' Create a new GridView object.
    Dim customersGridView As New
 GridView()

    ' Set the GridView object's properties.
    customersGridView.ID = "CustomersGridView"
    customersGridView.DataSourceID = "CustomersSource"
    customersGridView.AutoGenerateColumns = False

    ' Dynamically create the columns for the GridView control.
    Dim addColumn As New
 ButtonField()
    addColumn.CommandName = "Add"
    addColumn.Text = "Add"
    addColumn.ButtonType = ButtonType.Link

    Dim companyNameColumn As New
 BoundField()
    companyNameColumn.DataField = "CompanyName"
    companyNameColumn.HeaderText = "Company Name"

    Dim cityColumn As New
 BoundField()
    cityColumn.DataField = "City"
    cityColumn.HeaderText = "City"

    ' Add the columns to the Columns collection
    ' of the GridView control.
    customersGridView.Columns.Add(addColumn)
    customersGridView.Columns.Add(companyNameColumn)
    customersGridView.Columns.Add(cityColumn)

    ' Programmatically register the event handling methods.
    AddHandler customersGridView.RowCommand, AddressOf
 CustomersGridView_RowCommand
    AddHandler customersGridView.RowCreated, AddressOf
 CustomersGridView_RowCreated

    ' Add the GridView object to the Controls collection
    ' of the PlaceHolder control.
    GridViewPlaceHolder.Controls.Add(customersGridView)

  End Sub

  Sub CustomersGridView_RowCommand(ByVal sender
 As Object, ByVal e As
 GridViewCommandEventArgs)
  
    ' If multiple ButtonField columns are used, 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. Use the
      ' CommandSource property to access the GridView control.
      Dim customersGridView As GridView = CType(e.CommandSource,
 GridView)
      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(1).Text) + " "
 + Server.HtmlDecode(row.Cells(2).Text)

      ' If the author is not already in the ListBox, add the ListItem
      ' object to the Items collection of a 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 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>
  <body>
    <form runat="server">

      <h3>GridViewCommandEventHandler Example</h3>

      <table width="100%">
        <tr>
          <td width="50%">
            <asp:placeholder id="GridViewPlaceHolder"
              runat="Server"/>
          </td>

          <td valign="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>

<%@ Page language="C#" %>

<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {

    // Create a new GridView object.
    GridView customersGridView = new GridView();

    // Set the GridView object's properties.
    customersGridView.ID = "CustomersGridView";
    customersGridView.DataSourceID = "CustomersSource";
    customersGridView.AutoGenerateColumns = false;

    // Dynamically create the columns for the GridView control.
    ButtonField addColumn = new ButtonField();
    addColumn.CommandName = "Add";
    addColumn.Text = "Add";
    addColumn.ButtonType = ButtonType.Link;

    BoundField companyNameColumn = new BoundField();
    companyNameColumn.DataField = "CompanyName";
    companyNameColumn.HeaderText = "Company Name";

    BoundField cityColumn = new BoundField();
    cityColumn.DataField = "City";
    cityColumn.HeaderText = "City";

    // Add the columns to the Columns collection
    // of the GridView control.
    customersGridView.Columns.Add(addColumn);
    customersGridView.Columns.Add(companyNameColumn);
    customersGridView.Columns.Add(cityColumn);

    // Programmatically register the event handling methods.
    customersGridView.RowCommand += new GridViewCommandEventHandler(this.CustomersGridView_RowCommand);
    customersGridView.RowCreated += new GridViewRowEventHandler(this.CustomersGridView_RowCreated);

    // Add the GridView object to the Controls collection
    // of the PlaceHolder control.
    GridViewPlaceHolder.Controls.Add(customersGridView);

  }

  void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs
 e)
  {
    // If multiple ButtonField columns are used, 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. Use the
      // CommandSource property to access the GridView control.
      GridView customersGridView = (GridView)e.CommandSource;
      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[1].Text) + " " + Server.HtmlDecode(row.Cells[2].Text);

      // If the author is not already in the ListBox, add the ListItem
      // object to the Items collection of a 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 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>
  <body>
    <form runat="server">

      <h3>GridViewCommandEventHandler Example</h3>

      <table width="100%">
        <tr>
          <td width="50%">
            <asp:placeholder id="GridViewPlaceHolder"
              runat="Server"/>
          </td>

          <td valign="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>

GridViewCommandEventHandler デリゲートを、宣言によって GridView コントロールRowCommand イベント追加する方法次の例に示します

<%@ Page language="VB" %>

<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>
  <body>
    <form runat="server">
        
      <h3>GridView RowCommand Example</h3>
            
      <table width="100%">         
        <tr>                
          <td 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 valign="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>

<%@ Page language="C#" %>

<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>
  <body>
    <form runat="server">
        
      <h3>GridView RowCommand Example</h3>
            
      <table width="100%">         
        <tr>                
          <td 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 valign="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>

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
System.Web.UI.WebControls 名前空間
GridView クラス
GridViewCommandEventArgs クラス
GridView.RowCommand イベント
OnRowCommand
その他の技術情報
イベントデリゲート


このページでは「.NET Framework クラス ライブラリ リファレンス」からGridViewCommandEventHandler デリゲートを検索した結果を表示しています。
Weblioに収録されているすべての辞書からGridViewCommandEventHandler デリゲートを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からGridViewCommandEventHandler デリゲート を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

GridViewCommandEventHandler デリゲートのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



GridViewCommandEventHandler デリゲートのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS