Export (0) Print
Expand All
6 out of 9 rated this helpful - Rate this topic

GridViewRow Class

Note: This class is new in the .NET Framework version 2.0.

Represents an individual row in a GridView control.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public class GridViewRow : TableRow, IDataItemContainer, INamingContainer
public class GridViewRow extends TableRow implements IDataItemContainer, INamingContainer
public class GridViewRow extends TableRow implements IDataItemContainer, INamingContainer

The GridViewRow class is used represent an individual row in a GridView control. Each row in the GridView control has a designated row type. The following table lists the different row types.

Row type

Description

DataGridRowType.DataRow

A data row in the GridView control.

DataGridRowType.Footer

The footer row in the GridView control.

DataGridRowType.Header

The header row in the GridView control.

DataGridRowType.NullRow

The null row in the GridView control. The null row is displayed in a GridView control when there are no records to display.

DataGridRowType.Pager

A pager row in the GridView control.

DataGridRowType.Separator

A separator row in the GridView control.

To determine the row type of a GridViewRow object, use the RowType property. A GridViewRow object also has a state associated with it. The state can be a bitwise combination of the values in the following table.

State value

Description

DataControlRowState.Alternate

The GridViewRow object is an alternate row in the GridView control.

DataControlRowState.Edit

The GridViewRow object is in edit mode.

DataControlRowState.Normal

The GridViewRow object is in its normal (default) state.

DataControlRowState.Selected

The GridViewRow object is selected.

To determine the state of a GridViewRow object, use the RowState property.

The GridView control stores all of its data rows in the Rows collection. To determine the index of a GridViewRow object in the Rows collection, use the RowIndex property.

You can access the properties of the underlying data object that is bound to the GridViewRow object by using the DataItem property.

NoteNote

The DataItem property is only available during and after the RowDataBound event of a GridView control.

To determine the index of the data object in the underlying data source, use the DataItemIndex property.

You can access the individual cells of the GridViewRow object by using the Cells property. If a cell contains other controls, you can retrieve a control from the cell by using the Controls collection of the cell. You can also use the FindControl method of the cell to find the control, if the control has an ID specified.

To retrieve a field value from a BoundField field column or an automatically generated field column, use the Text property of the cell. To retrieve a field value from other field column types where the field value is bound to a control, first retrieve the control from the appropriate cell and then access the appropriate property of the control.

NoteNote

It is possible to use a data-binding expression directly in a TemplateField field column without binding the value to a property of a control. In this case, the field value is automatically placed in a DataBoundLiteralControl control. To retrieve the field value, you must first retrieve the DataBoundLiteralControl control from the appropriate cell and then use its Text property.

For a list of initial property values for an instance of GridViewRow, see the GridViewRow constructor.

The following example demonstrates how to use a GridViewRow object to retrieve a field value from a cell in the GridView control and then display the value on the page.


<%@ 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>


The following example demonstrates how to use a GridViewRow object to retrieve a TextBox control declared in a TemplateField field column's edit item template. The value of the text box is then passed to a SqlDataSource control for updating in the data source.

<%@ 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>


System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
       System.Web.UI.WebControls.TableRow
        System.Web.UI.WebControls.GridViewRow
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.