This documentation is archived and is not being maintained.

HtmlTableCellCollection Class

A collection of HtmlTableCell objects that represent the cells in a single row of an HtmlTable control. This class cannot be inherited.

For a list of all members of this type, see HtmlTableCellCollection Members.

System.Object
   System.Web.UI.HtmlControls.HtmlTableCellCollection

[Visual Basic]
NotInheritable Public Class HtmlTableCellCollection
   Implements ICollection, IEnumerable
[C#]
public sealed class HtmlTableCellCollection : ICollection,
   IEnumerable
[C++]
public __gc __sealed class HtmlTableCellCollection : public
   ICollection, IEnumerable
[JScript]
public class HtmlTableCellCollection implements ICollection,
   IEnumerable

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

Use the HtmlTableCellCollection class to programmatically manage a collection of HtmlTableCell objects that represent the cells of a single row in an HtmlTable control. This class is commonly used to add, remove, or modify the contents of a cell in a row of an HtmlTable control.

Note   An HtmlTable control contains a Rows property that contains a collection of HtmlTableRow objects. Each HtmlTableRow represents an individual row in the table. An HtmlTableRow contains a Cells property that represents a collection of HtmlTableCell objects. These objects, in turn, represent the individual cells of a row. To retrieve an individual cell, first get the HtmlTableRow that represents the row containing the cell in the table (from the Rows collection of the HtmlTable control). You can then get the HtmlTableCell that represents the cell in the row (from the Cells collection of the HtmlTableRow).

Example

[Visual Basic, C#, JScript] The following example demonstrates how to dynamically generate the contents of an HtmlTable control by adding cells to an HtmlTableCellCollection. Notice that the Cells property of a row, represented by an HtmlTableRow, is the HtmlTableCellCollection.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      Sub Page_Load(sender As Object, e As EventArgs)

         Dim i As Integer
         Dim j As Integer
         Dim row As HtmlTableRow
         Dim cell As HtmlTableCell 

         ' Get the number of rows and columns selected by the user.
         Dim numrows As Integer = CInt(Select1.Value)
         Dim numcells As Integer = CInt(Select2.Value)

         ' Iterate through the rows.
         For j = 0 to numrows - 1 

            ' Create a new row and add it to the Rows collection.
            row = New HtmlTableRow()

            ' Provide a different background color for alternating rows.
            If (j Mod 2) = 1 Then
               row.BgColor="Gainsboro"
            End If

            ' Iterate through the cells of a row.
            For i = 0 to numcells - 1 
           
               ' Create a new cell and add it to the Cells collection.
               cell = New HtmlTableCell()
               cell.Controls.Add(new LiteralControl("row " & _ 
                                                 j.ToString() & _ 
                                                 ", cell " & _
                                                 i.ToString()))
               row.Cells.Add(cell)
            
            Next i

            Table1.Rows.Add(row)
         
         Next j
      
      End Sub

   </script>

</head>
<body>

   <form runat="server">

      <h3>HtmlTableCellCollection Example</h3>

      <table id="Table1" 
             CellPadding="5" 
             CellSpacing="0" 
             Border="1" 
             BorderColor="black" 
             runat="server"/>
        
      <hr>

      Select the number of rows and columns to create: <br><br>

      Table rows:
      <select id="Select1" 
              runat="server">

         <option Value="1">1</option>
         <option Value="2">2</option>
         <option Value="3">3</option>
         <option Value="4">4</option>
         <option Value="5">5</option>

      </select>

      &nbsp;&nbsp;

      Table cells:
      <select id="Select2" 
              runat="server">

         <option Value="1">1</option>
         <option Value="2">2</option>
         <option Value="3">3</option>
         <option Value="4">4</option>
         <option Value="5">5</option>

      </select>
       
      <br><br>
  
      <input type="submit" 
             value="Generate Table" 
             runat="server"/>

   </form>

</body>
</html>

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

<html>
<head>

   <script runat="server">

      void Page_Load(Object sender, EventArgs e) 
      {

         // Get the number of rows and columns selected by the user.
         int numrows = Convert.ToInt32(Select1.Value);
         int numcells = Convert.ToInt32(Select2.Value);

         // Iterate through the rows.
         for (int j=0; j<numrows; j++) 
         {

            // Create a new row and add it to the Rows collection.
            HtmlTableRow row = new HtmlTableRow();

            // Provide a different background color for alternating rows.
            if (j%2 == 1)
               row.BgColor="Gainsboro";

            // Iterate through the cells of a row.
            for (int i=0; i<numcells; i++) 
            {
               // Create a new cell and add it to the Cells collection.
               HtmlTableCell cell = new HtmlTableCell();
               cell.Controls.Add(new LiteralControl("row " + 
                                                 j.ToString() + 
                                                 ", cell " +
                                                 i.ToString()));
               row.Cells.Add(cell);
            }
            Table1.Rows.Add(row);
         }
      }

   </script>

</head>
<body>

   <form runat="server">

      <h3>HtmlTableCellCollection Example</h3>

      <table id="Table1" 
             CellPadding="5" 
             CellSpacing="0" 
             Border="1" 
             BorderColor="black" 
             runat="server"/>
        
      <hr>

      Select the number of rows and columns to create: <br><br>

      Table rows:
      <select id="Select1" 
              runat="server">

         <option Value="1">1</option>
         <option Value="2">2</option>
         <option Value="3">3</option>
         <option Value="4">4</option>
         <option Value="5">5</option>

      </select>

      &nbsp;&nbsp;

      Table cells:
      <select id="Select2" 
              runat="server">

         <option Value="1">1</option>
         <option Value="2">2</option>
         <option Value="3">3</option>
         <option Value="4">4</option>
         <option Value="5">5</option>

      </select>
       
      <br><br>
  
      <input type="submit" 
             value="Generate Table" 
             runat="server"/>

   </form>

</body>
</html>

[JScript] 
<%@ Page Language="JScript" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      function Page_Load(sender : Object, e : EventArgs) 
      {

         // Get the number of rows and columns selected by the user.
         var numrows : int = Convert.ToInt32(Select1.Value);
         var numcells : int = Convert.ToInt32(Select2.Value);

         // Iterate through the rows.
         for (var j : int = 0; j < numrows; j++) 
         {

            // Create a new row and add it to the Rows collection.
            var row : HtmlTableRow = new HtmlTableRow();

            // Provide a different background color for alternating rows.
            if (j%2 == 1)
               row.BgColor="Gainsboro";

            // Iterate through the cells of a row.
            for (var i : int = 0; i < numcells; i++) 
            {
               // Create a new cell and add it to the Cells collection.
               var cell : HtmlTableCell = new HtmlTableCell();
               cell.Controls.Add(new LiteralControl("row " + 
                                                 j.ToString() + 
                                                 ", cell " +
                                                 i.ToString()));
               row.Cells.Add(cell);
            }
            Table1.Rows.Add(row);
         }
      }

   </script>

</head>
<body>

   <form runat="server">

      <h3>HtmlTableCellCollection Example</h3>

      <table id="Table1" 
             CellPadding="5" 
             CellSpacing="0" 
             Border="1" 
             BorderColor="black" 
             runat="server"/>
        
      <hr>

      Select the number of rows and columns to create: <br><br>

      Table rows:
      <select id="Select1" 
              runat="server">

         <option Value="1">1</option>
         <option Value="2">2</option>
         <option Value="3">3</option>
         <option Value="4">4</option>
         <option Value="5">5</option>

      </select>

      &nbsp;&nbsp;

      Table cells:
      <select id="Select2" 
              runat="server">

         <option Value="1">1</option>
         <option Value="2">2</option>
         <option Value="3">3</option>
         <option Value="4">4</option>
         <option Value="5">5</option>

      </select>
       
      <br><br>
  
      <input type="submit" 
             value="Generate Table" 
             runat="server"/>

   </form>

</body>
</html>

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI.HtmlControls

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

HtmlTableCellCollection Members | System.Web.UI.HtmlControls Namespace | HtmlTable | Rows | HtmlTableRow | Cells | HtmlTableCell

Show: