DetailsView.Rows Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets a collection of DetailsViewRow objects that represent the data rows in a DetailsView control.

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

public virtual DetailsViewRowCollection Rows { get; }

Property Value

Type: System.Web.UI.WebControls.DetailsViewRowCollection

A DetailsViewRowCollection that contains all the data rows in a DetailsView control.

The Rows property (collection) is used to store the data rows in a DetailsView control. The DetailsView control automatically populates the Rows collection by creating a DetailsViewRow object for each record in the data source and then adding the 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.


Only data rows are stored in the Rows collection. The DetailsViewRow objects that represent the header, footer, and pager rows are not included in the collection.

The following code example demonstrates how to use the Rows property to access a data row in a DetailsView control. The data row is used to retrieve the value of a field from the DetailsView control.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

    void CustomerDetailView_ItemCommand(Object sender, DetailsViewCommandEventArgs e)

        // Use the CommandName property to determine which button
        // was clicked. 
        if (e.CommandName == "Add")

            // Add the current store to the contact list. 

            // Get the row that contains the store name. In this
            // example, the store name is in the second row (index 1)  
            // of the DetailsView control.
            DetailsViewRow row = CustomerDetailView.Rows[1];

            // Get the store's name from the appropriate cell.
            // In this example, the store name is in the second cell  
            // (index 1) of the row.
            String name = row.Cells[1].Text;

            // Create a ListItem object with the store's name.
            ListItem item = new ListItem(name);

            // Add the ListItem object to the ListBox, if the 
            // item doesn't already exist.
            if (!ContactListBox.Items.Contains(item))




<html xmlns="" >
<head runat="server">
            DetailsView ItemCommand Example</title>
    <form id="Form1" runat="server">
            DetailsView ItemCommand Example</h3>
        <asp:DetailsView ID="CustomerDetailView" 

            <FieldHeaderStyle BackColor="Navy" ForeColor="White" />

                <asp:BoundField DataField="CustomerID" HeaderText="Store ID" />
                <asp:BoundField DataField="CompanyName" HeaderText="Store Name" />
                <asp:BoundField DataField="City" HeaderText="City" />
                <asp:ButtonField CommandName="Add" Text="Add Contact" />

        <hr />

        Contacts:<br />
        <asp:ListBox ID="ContactListBox" runat="server" />
        <!-- 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="DetailsViewSource" runat="server" 
            "<%$ ConnectionStrings:NorthWindConnectionString%>"
            InsertCommand="INSERT INTO [Customers]([CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country]) VALUES (@CustomerID, @CompanyName, @Address, @City, @PostalCode, @Country)"
          SelectCommand="Select [CustomerID], [CompanyName], 
            [Address], [City], [PostalCode], [Country] From 

.NET Framework
Available since 2.0
Return to top