HtmlTableCellCollection Class

Definition

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

public ref class HtmlTableCellCollection sealed : System::Collections::ICollection
public sealed class HtmlTableCellCollection : System.Collections.ICollection
type HtmlTableCellCollection = class
    interface ICollection
    interface IEnumerable
Public NotInheritable Class HtmlTableCellCollection
Implements ICollection
Inheritance
HtmlTableCellCollection
Implements

Examples

The following code example demonstrates how to dynamically generate the contents of an HtmlTable control by adding cells to an HtmlTableCellCollection collection. Notice that the Cells property of a row, represented by an HtmlTableRow object, is the HtmlTableCellCollection collection.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 = "Gray";

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
   <title>HtmlTableCellCollection Example</title>
</head>
<body>

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

      <h3>HtmlTableCellCollection Example</h3>

      <table id="Table1" 
             style="border-width:1; border-color:Black; padding:5"
             cellspacing="0" 
             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>

        

      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>
<%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
  Sub Page_Load(ByVal sender As Object, ByVal 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 = "Gray"
      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>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
   <title>HtmlTableCellCollection Example</title>
</head>
<body>

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

      <h3>HtmlTableCellCollection Example</h3>

      <table id="Table1" 
             style="border-width:1; border-color:Black; padding:5"
             cellspacing="0" 
             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>

        

      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>

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 object represents an individual row in the table. An HtmlTableRow object 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 object that represents the row containing the cell in the table (from the Rows collection of the HtmlTable control). You can then get the HtmlTableCell object that represents the cell in the row (from the Cells collection of the HtmlTableRow object).

Properties

Count

Gets the number of HtmlTableCell objects in the HtmlTableCellCollection collection.

IsReadOnly

Gets a value indicating whether the HtmlTableCellCollection collection is read-only.

IsSynchronized

Gets a value indicating whether access to the HtmlTableCellCollection collection is synchronized (thread safe).

Item[Int32]

Gets the HtmlTableCell object at the specified index from the HtmlTableCellCollection collection.

SyncRoot

Gets the object that can be used to synchronize access to the HtmlTableCellCollection collection.

Methods

Add(HtmlTableCell)

Appends the specified HtmlTableCell object to the end of the HtmlTableCellCollection collection.

Clear()

Removes all HtmlTableCell objects from the HtmlTableCellCollection collection.

CopyTo(Array, Int32)

Copies the items from the HtmlTableCellCollection collection to the specified Array, beginning with the specified index in the Array.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator()

Returns a IEnumerator-implemented object that contains all HtmlTableCell objects in the HtmlTableCellCollection collection.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
Insert(Int32, HtmlTableCell)

Adds the specified HtmlTableCell object at the specified index location of the HtmlTableCellCollection collection.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(HtmlTableCell)

Removes the specified HtmlTableCell object from the HtmlTableCellCollection collection.

RemoveAt(Int32)

Removes the HtmlTableCell object at the specified index from the HtmlTableCellCollection collection.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Extension Methods

Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

See also