Export (0) Print
Expand All

HtmlTableCellCollection Class

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

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

'Declaration
Public NotInheritable Class HtmlTableCellCollection
	Implements ICollection, IEnumerable
'Usage
Dim instance As HtmlTableCellCollection

public final class HtmlTableCellCollection implements ICollection, IEnumerable
public final class HtmlTableCellCollection implements ICollection, IEnumerable

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.

NoteNote

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).

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

<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>
<head>
   <title>HtmlTableCellCollection Example</title>
</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>

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

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

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft