Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
GridView Class
 Rows Property
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
GridView..::.Rows Property

Updated: November 2007

Gets a collection of GridViewRow objects that represent the data rows in a GridView control.

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

Visual Basic (Declaration)
<BrowsableAttribute(False)> _
Public Overridable ReadOnly Property Rows As GridViewRowCollection
Visual Basic (Usage)
Dim instance As GridView
Dim value As GridViewRowCollection

value = instance.Rows
C#
[BrowsableAttribute(false)]
public virtual GridViewRowCollection Rows { get; }
Visual C++
[BrowsableAttribute(false)]
public:
virtual property GridViewRowCollection^ Rows {
    GridViewRowCollection^ get ();
}
J#
/** @property */
/** @attribute BrowsableAttribute(false) */
public GridViewRowCollection get_Rows()
JScript
public function get Rows () : GridViewRowCollection

Property Value

Type: System.Web.UI.WebControls..::.GridViewRowCollection

A GridViewRowCollection that contains all the data rows in a GridView control.

The Rows property (collection) is used to store the data rows in a GridView control. The GridView control automatically populates the Rows collection by creating a GridViewRow object for each record in the data source and then adding each object to the collection. This property is commonly used to access a specific row in the control or to iterate though the entire collection of rows.

Note:

Only rows with their RowType property set to DataControlRowType.DataRow are stored in the Rows collection. The GridViewRow objects that represent the header, footer, and pager rows are not included in the collection.

The following example demonstrates how to use the Rows collection to access the row being edited in a GridView control. After a row is updated, a message is displayed to indicate that the update was successful.

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)

    ' Clear the message label when the user enters edit mode.
    If e.CommandName = "Edit" Then
      Message.Text = ""
    End If

  End Sub

  Sub CustomersGridView_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)

    ' The update operation was successful. Retrieve the row being edited.
    Dim index As Integer = CustomersGridView.EditIndex
    Dim row As GridViewRow = CustomersGridView.Rows(index)

    ' Notify the user that the update was successful.
    Message.Text = "Updated record " & row.Cells(1).Text + "."

  End Sub

  Sub CustomersGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)

    ' The update operation was canceled. Display the appropriate message.
    Message.Text = "Update operation canceled."

  End Sub

</script>

<html  >
  <head runat="server">
    <title>GridView Rows Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView Rows Example</h3>

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

      <br/>    

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView"
        allowpaging="true" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogenerateeditbutton="true"
        datakeynames="CustomerID"
        onrowcommand="CustomersGridView_RowCommand"
        onrowupdated="CustomersGridView_RowUpdated"
        onrowcancelingedit="CustomersGridView_RowCancelingEdit"  
        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="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>

    </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)
  {

    // Clear the message label when the user enters edit mode.
    if (e.CommandName == "Edit")
    {
      Message.Text = "";
    }

  }

  void CustomersGridView_RowUpdated(Object sender, GridViewUpdatedEventArgs e)
    {

        // The update operation was successful. Retrieve the row being edited.
        int index = CustomersGridView.EditIndex;
        GridViewRow row = CustomersGridView.Rows[index];

        // Notify the user that the update was successful.
        Message.Text = "Updated record " + row.Cells[1].Text + ".";

    }

  void CustomersGridView_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
    {

        // The update operation was canceled. Display the appropriate message.
        Message.Text = "Update operation canceled.";

    }

</script>

<html  >
  <head runat="server">
    <title>GridView Rows Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView Rows Example</h3>

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

      <br/>    

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView"
        allowpaging="true" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogenerateeditbutton="true"
        datakeynames="CustomerID"
        onrowcommand="CustomersGridView_RowCommand"
        onrowupdated="CustomersGridView_RowUpdated"
        onrowcancelingedit="CustomersGridView_RowCancelingEdit"  
        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="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>

    </form>
  </body>
</html>


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

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

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker