HtmlTableCellCollection.Count Property


Gets the number of HtmlTableCell objects in the HtmlTableCellCollection 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 HtmlTableCell objects in the HtmlTableCellCollection. The default value is 0.

Use the Count property to determine the number of cells contained in the HtmlTableCellCollection 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 cells in a row of the HtmlTable control. This value is then used as the upper bound of a loop to iterate through the cells of a row in the 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>HtmlTableCellCollection Example</title>

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

      <h3>HtmlTableCellCollection Example</h3>

      <table id="Table1" 
             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