HtmlTableCellCollection 類別

定義

HtmlTableCell 物件的集合,這些物件表示 HtmlTable 控制項中單一列的儲存格。 此類別無法獲得繼承。

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
繼承
HtmlTableCellCollection
實作

範例

下列程式碼範例示範如何將儲存格新增至 HtmlTableCellCollection 集合,以動態方式產生控制項的內容 HtmlTable 。 請注意, Cells 物件所 HtmlTableRow 表示之資料列的 屬性是 HtmlTableCellCollection 集合。

<%@ 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>

備註

HtmlTableCellCollection使用 類別,以程式設計方式管理 物件集合 HtmlTableCell ,這些物件代表控制項中 HtmlTable 單一資料列的儲存格。 這個類別通常用於新增、移除或修改控制項資料列中 HtmlTable 儲存格的內容。

注意

HtmlTable控制項包含 Rows 包含 物件集合的屬性 HtmlTableRow 。 每個 HtmlTableRow 物件都代表資料表中的個別資料列。 HtmlTableRow物件包含 Cells 代表 物件集合的屬性 HtmlTableCell 。 這些物件接著代表資料列的個別儲存格。 若要擷取個別儲存格,請先從 RowsHtmlTable 控制項集合中取得 HtmlTableRow 物件,該物件代表資料表中含有儲存格的資料列 () 。 然後,您可以從物件集合 HtmlTableRow) ,取得 HtmlTableCell 代表資料列中 Cells 儲存格的 物件 (。

屬性

Count

取得 HtmlTableCellCollection 集合中 HtmlTableCell 物件的數目。

IsReadOnly

取得值,表示 HtmlTableCellCollection 集合是否為唯讀。

IsSynchronized

取得值,指出對 HtmlTableCellCollection 集合的存取是否為同步 (具備執行緒安全)。

Item[Int32]

取得 HtmlTableCell 集合中位於指定索引處的 HtmlTableCellCollection 物件。

SyncRoot

取得可用來同步存取 HtmlTableCellCollection 集合的物件。

方法

Add(HtmlTableCell)

將指定的 HtmlTableCell 物件附加到 HtmlTableCellCollection 集合的結尾。

Clear()

HtmlTableCell 集合移除所有的 HtmlTableCellCollection 物件。

CopyTo(Array, Int32)

將項目從 HtmlTableCellCollection 複製到指定的 Array,從 Array 中的指定索引處開始。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回 IEnumerator 實作的物件,其中包含 HtmlTableCell 集合中所有的 HtmlTableCellCollection 物件。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
Insert(Int32, HtmlTableCell)

將指定的 HtmlTableCell 物件加入至 HtmlTableCellCollection 集合的指定索引位置。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove(HtmlTableCell)

HtmlTableCell 集合中移除指定的 HtmlTableCellCollection 物件。

RemoveAt(Int32)

HtmlTableCell 集合中移除指定索引處的 HtmlTableCellCollection 物件。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱