GridViewRow クラスとは? わかりやすく解説

GridViewRow クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

GridView コントロール個別の行を表します

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

Public Class GridViewRow
    Inherits TableRow
    Implements IDataItemContainer, INamingContainer
public class GridViewRow : TableRow, IDataItemContainer,
 INamingContainer
public ref class GridViewRow : public
 TableRow, IDataItemContainer, INamingContainer
public class GridViewRow extends TableRow implements
 IDataItemContainer, INamingContainer
public class GridViewRow extends
 TableRow implements IDataItemContainer, INamingContainer
解説解説

GridViewRow クラスは、GridView コントロール個別の行を表すために使用されます。GridView コントロール各行には、行の種類指定されています。さまざまな行の種類の一覧次の表に示します

行の種類

説明

DataGridRowType.DataRow

GridView コントロールデータ行。

DataGridRowType.Footer

GridView コントロールフッター行。

DataGridRowType.Header

GridView コントロールヘッダー行。

DataGridRowType.NullRow

GridView コントロールnull 行。null 行は、表示するレコードない場合GridView コントロール表示されます。

DataGridRowType.Pager

GridView コントロールページ行。

DataGridRowType.Separator

GridView コントロール区切り行。

GridViewRow オブジェクトの行の種類確認するには、RowType プロパティ使用しますGridViewRow オブジェクトには、状態も関連付けられます。状態は、次の表に示す値のビットごとの組み合わせなります

状態の値

説明

DataControlRowState.Alternate

GridViewRow オブジェクトは、GridView コントロール交互の行です。

DataControlRowState.Edit

GridViewRow オブジェクト編集モードです。

DataControlRowState.Normal

GridViewRow オブジェクト標準 (既定) の状態です。

DataControlRowState.Selected

GridViewRow オブジェクト選択されています。

GridViewRow オブジェクトの状態を確認するには、RowState プロパティ使用します

GridView コントロールは、すべてのデータ行を Rows コレクション格納しますRows コレクション内の GridViewRow オブジェクトインデックス確認するには、RowIndex プロパティ使用します

DataItem プロパティ使用してGridViewRow オブジェクトバインドされている基になるデータ オブジェクトプロパティアクセスできます

メモメモ

DataItem プロパティは、GridView コントロールの RowDataBound イベントの発生中および発生後にしか使用できません。

基になるデータ ソースデータ オブジェクトインデックス確認するには、DataItemIndex プロパティ使用します

Cells プロパティ使用してGridViewRow オブジェクト個別セルアクセスできますセルに他のコントロール格納されている場合は、セルControls コレクション使用してセルからコントロール取得できますコントロールID指定されている場合は、セルの FindControl メソッド使用してコントロール検索することもできます

BoundField フィールド列または自動生成されたフィールド列からフィールド値を取得するには、セルText プロパティ使用しますフィールド値がコントロールバインドされている他の種類フィールド列からフィールド値を取得するには、該当するセルからコントロール取得してから、コントロール該当するプロパティアクセスます。

GridViewRowインスタンス初期プロパティ値の一覧については、GridViewRow コンストラクタトピック参照してください

使用例使用例

GridViewRow オブジェクト使用してGridView コントロールセルからフィールド値を取得し、その値をページ表示する方法次の例に示します

<%@ Page language="VB" %>

<script runat="server">

  Sub AuthorsGridView_SelectedIndexChanged(ByVal
 sender As Object, ByVal
 e As EventArgs)
    
    ' Get the selected row from the GridView control.
    Dim selectRow As GridViewRow = AuthorsGridView.SelectedRow
    
    ' Get the author's first and last name from the appropriate
    ' cells in the selected row. For BoundField field columns
    ' and automatically generated field columns, the Text property
    ' of a cell is used to access a field value.
    Dim lastName As String
 = selectRow.Cells(1).Text
    
    ' In a TemplateField column where a data-binding expression
    ' is used directly in the ItemTemplate, the field value
    ' is automatically placed in DataBoundLiteral control.
    
    ' Retrieve the DataBoundLiteral control from the cell. The
    ' DataBoundLiteral control is the first control in the 
    ' Controls collection.
    Dim firstNameLiteral As DataBoundLiteralControl
 = CType(selectRow.Cells(2).Controls(0), DataBoundLiteralControl)
    Dim firstName As String
 = firstNameLiteral.Text
    
    ' Display the name of the selected author.
    Message.Text = "You selected " & firstName
 & " " & lastName & "."
    
  End Sub

</script>

<html>
  <body>
    <form runat="server">
        
      <h3>GridViewRow Example</h3>

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

      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="false"
        autogenerateselectbutton="true"
        onselectedindexchanged="AuthorsGridView_SelectedIndexChanged"
  
        runat="server"> 
               
        <columns>
          <asp:boundfield datafield="au_lname"
            headertext="Last Name"/>
          <asp:templatefield headertext="FirstName">
            <itemtemplate>
              <%#Eval("au_fname")%>
            </itemtemplate>
          </asp:templatefield>
        </columns>
                              
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects
 -->
      <!-- to the Pubs sample database.                   
     -->
      <asp:sqldatasource id="AuthorsSqlDataSource"
  
        selectcommand="SELECT [au_lname], [au_fname], [address],
 [city], [state], [zip], [contract] FROM [authors]"
        connectionstring="server=localhost;database=pubs;integrated
 security=SSPI"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

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

<script runat="server">

  void AuthorsGridView_SelectedIndexChanged(Object sender, EventArgs
 e)
  {
  
    // Get the selected row from the GridView control.
    GridViewRow selectRow = AuthorsGridView.SelectedRow;
    
    // Get the author's first and last name from the appropriate
    // cells in the selected row. For BoundField field columns
    // and automatically generated field columns, the Text property
    // of a cell is used to access a field value.
    String lastName = selectRow.Cells[1].Text;
    
    // In a TemplateField column where a data-binding expression
    // is used directly in the ItemTemplate, the field value
    // is automatically placed in DataBoundLiteral control.
    
    // Retrieve the DataBoundLiteral control from the cell. The
    // DataBoundLiteral control is the first control in the 
    // Controls collection.
    DataBoundLiteralControl firstNameLiteral = (DataBoundLiteralControl)selectRow.Cells[2].Controls[0];
    String firstName = firstNameLiteral.Text;
    
    // Display the name of the selected author.
    Message.Text = "You selected " + firstName + " " + lastName
 + ".";
    
  }

</script>

<html>
  <body>
    <form runat="server">
        
      <h3>GridViewRow Example</h3>

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

      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="false"
        autogenerateselectbutton="true"
        onselectedindexchanged="AuthorsGridView_SelectedIndexChanged" 
 
        runat="server"> 
               
        <columns>
          <asp:boundfield datafield="au_lname"
            headertext="Last Name"/>
          <asp:templatefield headertext="FirstName">
            <itemtemplate>
              <%#Eval("au_fname")%>
            </itemtemplate>
          </asp:templatefield>
        </columns>
                              
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_lname], [au_fname], [address], [city], [state],
 [zip], [contract] FROM [authors]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

GridViewRow オブジェクト使用してTemplateField フィールド列の項目編集テンプレート宣言されている TextBox コントロール取得する方法次の例に示しますその後データ ソース更新するために、テキスト ボックスの値が SqlDataSource コントロール渡されます。

<%@ Page language="VB" %>

<script runat="server">
  
  Sub AuthorsGridView_RowUpdating(ByVal sender
 As Object, ByVal e As
 GridViewUpdateEventArgs)
    
    ' The GridView control does not automatically extract updated values
 
    ' from TemplateField column fields. These values must be added manually
 
    ' to the NewValues dictionary.
    
    ' Get the GridViewRow object that represents the row being edited
    ' from the Rows collection of the GridView control.
    Dim index As Integer
 = AuthorsGridView.EditIndex
    Dim row As GridViewRow = AuthorsGridView.Rows(index)
    
    ' Get the controls that contain the updated values. In this
    ' example, the updated values are contained in the TextBox 
    ' controls declared in the EditItemTemplates of the TemplateField
 
    ' column fields in the GridView control.
    Dim lastName As TextBox = CType(row.FindControl("LastNameTextBox"),
 TextBox)
    Dim firstName As TextBox = CType(row.FindControl("FirstNameTextBox"),
 TextBox)
    
    ' Add the updated values to the NewValues dictionary. Use the
    ' parameter names declared in the parameterized update query 
    ' string for the key names.
    e.NewValues("au_lname") = lastName.Text
    e.NewValues("au_fname") = firstName.Text
          
  End Sub

</script>

<html>
  <body>
    <form runat="server">
        
      <h3>GridViewRow Example</h3>

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames attribute as
 read-only    -->
      <!-- No input controls are rendered for these columns
 in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="false"
        autogenerateeditbutton="true"
        datakeynames="au_id"
        cellpadding="10"
        onrowupdating="AuthorsGridView_RowUpdating"
      
        runat="server">
                
        <columns>
        
          <asp:boundfield datafield="au_id"
            headertext="Author ID"
            readonly="true"/>
            
          <asp:templatefield headertext="Last Name"
            itemstyle-verticalalign="Top">
            
            <itemtemplate>
              <%#Eval("au_lname")%>
            </itemtemplate>
            
            <edititemtemplate>
              <asp:textbox id="LastNameTextBox"
                text='<%#Eval("au_lname")%>'
                width="90"
                runat="server"/>
              <br/>
              <asp:requiredfieldvalidator id="LastNameRequiredValidator"
                controltovalidate="LastNameTextBox"
                display="Dynamic"
                text="Please enter a last name." 
                runat="server" />             
                         
            </edititemtemplate>
            
          </asp:templatefield>
          
          <asp:templatefield headertext="First Name"
            itemstyle-verticalalign="Top">
            
            <itemtemplate>
              <%#Eval("au_fname")%>
            </itemtemplate>
            
            <edititemtemplate>
              <asp:textbox id="FirstNameTextBox"
                text='<%#Eval("au_fname")%>'
                width="90"
                runat="server"/>
              <br/>
              <asp:requiredfieldvalidator id="FirstNameRequiredValidator"
                controltovalidate="FirstNameTextBox"
                display="Dynamic"
                text="Please enter a first name."
                runat="server" />             
         
            </edititemtemplate>
            
          </asp:templatefield>
          
          <asp:checkboxfield datafield="contract"
 
            headertext="Contract"
            readonly="true"/>
            
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects
 -->
      <!-- to the Pubs sample database.                   
      -->
      <asp:sqldatasource id="AuthorsSqlDataSource"
  
        selectcommand="SELECT [au_id], [au_lname], [au_fname],
 [contract] FROM [authors]"             
        updatecommand="UPDATE authors SET au_lname=@au_lname,
 au_fname=@au_fname WHERE (authors.au_id = @au_id)" 
        connectionstring="server=localhost;database=pubs;integrated
 security=SSPI"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

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

<script runat="server">
  
  void AuthorsGridView_RowUpdating (Object sender, GridViewUpdateEventArgs
 e)
  {
    
    // The GridView control does not automatically extract updated values
 
    // from TemplateField column fields. These values must be added
 manually 
    // to the NewValues dictionary.
    
    // Get the GridViewRow object that represents the row being edited
    // from the Rows collection of the GridView control.
    int index = AuthorsGridView.EditIndex;
    GridViewRow row = AuthorsGridView.Rows[index];
    
    // Get the controls that contain the updated values. In this
    // example, the updated values are contained in the TextBox 
    // controls declared in the EditItemTemplates of the TemplateField
 
    // column fields in the GridView control.
    TextBox lastName = (TextBox)row.FindControl("LastNameTextBox");
    TextBox firstName = (TextBox)row.FindControl("FirstNameTextBox");
    
    // Add the updated values to the NewValues dictionary. Use the
    // parameter names declared in the parameterized update query 
    // string for the key names.
    e.NewValues["au_lname"] = lastName.Text;
    e.NewValues["au_fname"] = firstName.Text;    
          
  }

</script>

<html>
  <body>
    <form runat="server">
        
      <h3>GridViewRow Example</h3>

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames attribute as read-only
    -->
      <!-- No input controls are rendered for these columns
 in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="false"
        autogenerateeditbutton="true"
        datakeynames="au_id"
        cellpadding="10"
        onrowupdating="AuthorsGridView_RowUpdating"      
        runat="server">
                
        <columns>
        
          <asp:boundfield datafield="au_id"
            headertext="Author ID"
            readonly="true"/>
            
          <asp:templatefield headertext="Last Name"
            itemstyle-verticalalign="Top">
            
            <itemtemplate>
              <%#Eval("au_lname")%>
            </itemtemplate>
            
            <edititemtemplate>
              <asp:textbox id="LastNameTextBox"
                text='<%#Eval("au_lname")%>'
                width="90"
                runat="server"/>
              <br/>
              <asp:requiredfieldvalidator id="LastNameRequiredValidator"
                controltovalidate="LastNameTextBox"
                display="Dynamic"
                text="Please enter a last name." 
                runat="server" />                                  
    
            </edititemtemplate>
            
          </asp:templatefield>
          
          <asp:templatefield headertext="First Name"
            itemstyle-verticalalign="Top">
            
            <itemtemplate>
              <%#Eval("au_fname")%>
            </itemtemplate>
            
            <edititemtemplate>
              <asp:textbox id="FirstNameTextBox"
                text='<%#Eval("au_fname")%>'
                width="90"
                runat="server"/>
              <br/>
              <asp:requiredfieldvalidator id="FirstNameRequiredValidator"
                controltovalidate="FirstNameTextBox"
                display="Dynamic"
                text="Please enter a first name."
                runat="server" />                      
            </edititemtemplate>
            
          </asp:templatefield>
          
          <asp:checkboxfield datafield="contract" 
            headertext="Contract"
            readonly="true"/>
            
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_id], [au_lname], [au_fname], [contract] FROM
 [authors]"             
        updatecommand="UPDATE authors SET au_lname=@au_lname, au_fname=@au_fname
 WHERE (authors.au_id = @au_id)" 
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

.NET Framework のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
       System.Web.UI.WebControls.TableRow
        System.Web.UI.WebControls.GridViewRow
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
GridViewRow メンバ
System.Web.UI.WebControls 名前空間
GridView クラス
GridViewRowCollection
GridView.Rows プロパティ
DataBoundLiteralControl
TableCell
Controls
FindControl
TableRow
Cells
DataItem
DataItemIndex
RowIndex
RowState
RowType



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

辞書ショートカット

すべての辞書の索引

「GridViewRow クラス」の関連用語

GridViewRow クラスのお隣キーワード
検索ランキング

   

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



GridViewRow クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS