HtmlTableRowCollection.Count Property


Gets the number of HtmlTableRow objects in the HtmlTableRowCollection collection.

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

public int Count { get; }

Property Value

Type: System.Int32

The number of HtmlTableRow objects in the HtmlTableRowCollection. The default value is 0.

Use the Count property to determine the number of rows contained in the HtmlTableRowCollection collection. The Count property is commonly used when iterating through the collection to determine the upper bound.

The following code example demonstrates how to use the Count property to determine the number of rows in the HtmlTable control. This value is then used as the upper bound of a loop to iterate through the rows of a table.

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

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

  void Button_Click(Object sender, EventArgs e)

    // Iterate through the rows of the table.
    for (int i = 0; i <= Table1.Rows.Count - 1; i++)

      // Iterate through the cells of a row.
      for (int j = 0; j <= Table1.Rows[i].Cells.Count - 1; j++)
        // Change the inner HTML of the cell.
        Table1.Rows[i].Cells[j].InnerHtml = "Row " + i.ToString() +
                                            ", Column " + j.ToString();




<html xmlns="" >
   <title>HtmlTableRowCollection Example</title>

   <form id="form1" runat="server">

      <h3>HtmlTableRowCollection Example</h3>

       <table id="Table1" runat="server" 
       style="border-width: 1; border-color: Black">

               Cell 1
               Cell 2
               Cell 3
               Cell 4


      <br /><br />

      <input type="button" 
             value="Change Table Contents"



.NET Framework
Available since 1.1
Return to top