HtmlTableRowCollection 類別

定義

HtmlTableRow 物件的集合,這些物件表示 HtmlTable 控制項的列。 此類別無法獲得繼承。

public ref class HtmlTableRowCollection sealed : System::Collections::ICollection
public sealed class HtmlTableRowCollection : System.Collections.ICollection
type HtmlTableRowCollection = class
    interface ICollection
    interface IEnumerable
Public NotInheritable Class HtmlTableRowCollection
Implements ICollection
繼承
HtmlTableRowCollection
實作

範例

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

<%@ 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>HtmlTableRowCollection Example</title>
</head>
<body>

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

      <h3>HtmlTableRowCollection 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>HtmlTableRowCollection Example</title>
</head>
<body>

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

      <h3>HtmlTableRowCollection 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>

備註

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

注意

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

屬性

Count

取得 HtmlTableRowCollection 集合中 HtmlTableRow 物件的數目。

IsReadOnly

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

IsSynchronized

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

Item[Int32]

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

SyncRoot

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

方法

Add(HtmlTableRow)

將指定的 HtmlTableRow 物件附加到 HtmlTableRowCollection 集合的結尾。

Clear()

HtmlTableRow 集合移除所有的 HtmlTableRowCollection 物件。

CopyTo(Array, Int32)

將項目從 HtmlTableRowCollection 集合複製到指定的 Array 物件,從陣列中的指定索引處開始。

Equals(Object)

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

(繼承來源 Object)
GetEnumerator()

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

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

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

HtmlTableRow 物件加入至集合中指定的位置。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove(HtmlTableRow)

HtmlTableRow 集合中移除指定的 HtmlTableRowCollection 物件。

RemoveAt(Int32)

HtmlTableRow 集合中移除指定索引處的 HtmlTableRowCollection 物件。

ToString()

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

(繼承來源 Object)

擴充方法

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱