TableCellCollection 類別

定義

封裝 TableHeaderCellTableCell 物件的集合,組成 Table 控制項中的單一資料列。 此類別無法獲得繼承。

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

範例

下列範例示範如何以程式設計方式填滿 Table 控制項。 TableCell 物件,代表個別儲存格,會透過 屬性新增至 TableRow 代表個別資料列的物件 Cells

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    private void Page_Load(Object sender, EventArgs e) 
    {
        // Generate rows and cells.           
        int numrows = 4;
        int numcells = 6;
        int counter = 1;
        for (int rowNum = 0; rowNum < numrows; rowNum++) 
        {          
            TableRow rw = new TableRow();
            for (int cellNum = 0; cellNum < numcells; cellNum++) 
            {
                TableCell cel = new TableCell();
                cel.Text=counter.ToString();
                counter++;
                rw.Cells.Add(cel);
            }
            Table1.Rows.Add(rw);
        }
    }
 
    private void Button_Click_Coord(object sender, EventArgs e) 
    {
        for (int rowNum = 0; rowNum < Table1.Rows.Count; rowNum++) 
        {          
            for (int cellNum = 0; cellNum < 
                Table1.Rows[rowNum].Cells.Count; cellNum++) 
            {
                Table1.Rows[rowNum].Cells[cellNum].Text = 
                    String.Format("(Row{0}, Cell{1})", rowNum, cellNum);
            }
        }
    }

    private void Button_Click_Number(object sender, EventArgs e) 
    {
        int counter = 1;
          
        for (int rowNum = 0; rowNum < Table1.Rows.Count; rowNum++) 
        {
            for (int cellNum = 0; cellNum < 
                Table1.Rows[rowNum].Cells.Count; cellNum++) 
            {
                Table1.Rows[rowNum].Cells[cellNum].Text = 
                    counter.ToString();
                counter++;
            }            
        }
    }
 
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>TableCellCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <h3>TableCellCollection Example</h3>
       <asp:Table id="Table1" 
            runat="server"/>
       <br />
       <center>
          <asp:Button id="Button1"
               Text="Display Table Coordinates"
               OnClick="Button_Click_Coord"
               runat="server"/>
          <asp:Button id="Button2"
               Text="Display Cell Numbers"
               OnClick="Button_Click_Number"
               runat="server"/>
       </center>
 
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Private Sub Page_Load(sender As Object, e As EventArgs)
        ' Generate rows and cells.           
        Dim numrows As Integer = 4
        Dim numcells As Integer = 6
        Dim counter As Integer = 1
        Dim rowNum As Integer
        Dim cellNum As Integer
        For rowNum = 0 To numrows - 1
            Dim rw As New TableRow()
            For cellNum = 0 To numcells - 1
                Dim cel As New TableCell()
                cel.Text = counter.ToString()
                counter += 1
                rw.Cells.Add(cel)
            Next
            Table1.Rows.Add(rw)
        Next
    End Sub

    Private Sub Button_Click_Coord(sender As Object, e As EventArgs)            
        Dim rowNum As Integer
        Dim cellNum As Integer
        Dim rowCount As Integer
        For rowCount = 0 To Table1.Rows.Count - 1
            For cellNum = 0 To (Table1.Rows(rowNum).Cells.Count) - 1                    
                Table1.Rows(rowNum).Cells(cellNum).Text = _
                    String.Format("(Row{0}, Cell{1})", rowNum, cellNum)
            Next
        Next
    End Sub

    Private Sub Button_Click_Number(sender As Object, e As EventArgs)
        Dim counter As Integer = 1

        Dim rowNum As Integer
        Dim cellNum As Integer
        For rowNum = 0 To Table1.Rows.Count - 1
            For cellNum = 0 To (Table1.Rows(rowNum).Cells.Count) - 1                    
                Table1.Rows(rowNum).Cells(cellNum).Text = _
                    counter.ToString()
                counter += 1
            Next 
        Next
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>TableCellCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <h3>TableCellCollection Example</h3>
       <asp:Table id="Table1" 
            runat="server"/>
       <br />
       <center>
          <asp:Button id="Button1"
               Text="Display Table Coordinates"
               OnClick="Button_Click_Coord"
               runat="server"/>
          <asp:Button id="Button2"
               Text="Display Cell Numbers"
               OnClick="Button_Click_Number"
               runat="server"/>
       </center>
 
    </div>
    </form>
</body>
</html>

備註

使用此類別以程式設計方式管理組成 控制項中 Table 資料列的物件 TableCell 集合。 這個類別通常用來新增或移除 控制項中資料列的 Table 儲存格。

注意

控制項 Table 包含 Rows 代表 物件集合的 TableRow 集合。 每個都 TableRow 代表資料表中的個別資料列,並包含 Cells 代表 物件集合的 TableCell 集合。 這些 TableCell 物件代表表格中的個別儲存格。 若要取得個別儲存格,您必須先從 Rows 控制項的 Table 集合取得 TableRow 。 然後,您可以從 的 TableRow 集合取得 。 Cells TableCell

屬性

Count

取得 TableCellTableCellCollection 物件的數目。

IsReadOnly

取得值,指出 TableCellCollection 是否唯讀。

IsSynchronized

取得值,該值指出是否同步存取 TableCellCollection (安全執行緒)。

Item[Int32]

從在指定的索引的 TableCell 取得 TableCellCollection

SyncRoot

取得可用來同步存取 TableCellCollection 的物件。

方法

Add(TableCell)

將指定的 TableCell 附加到 TableCellCollection 的末端。

AddAt(Int32, TableCell)

將指定的 TableCell 加入在指定索引位置的 TableCellCollection

AddRange(TableCell[])

TableCell 物件從指定的陣列附加至集合的結尾。

Clear()

TableCell 移除所有 TableCellCollection 物件。

CopyTo(Array, Int32)

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

Equals(Object)

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

(繼承來源 Object)
GetCellIndex(TableCell)

傳回表示 TableCell 中指定 TableCellCollection 的索引值。

GetEnumerator()

傳回 IEnumerator 實作的物件,其中包含 TableCell 中所有的 TableCellCollection 物件。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove(TableCell)

將指定的 TableCellTableCellCollection 中移除。

RemoveAt(Int32)

TableCell 從指定索引處的 TableCellCollection 移除。

ToString()

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

(繼承來源 Object)

明確介面實作

IList.Add(Object)

將物件加入至集合。

IList.Contains(Object)

判斷指定的物件是否包含在集合中。

IList.IndexOf(Object)

搜尋指定的物件,並傳回集合中第一個符合項目之以零起始的索引。

IList.Insert(Int32, Object)

插入物件至集合中的指定索引處。

IList.IsFixedSize

如需這個成員的說明,請參閱 IsFixedSize

IList.Item[Int32]

如需這個成員的說明,請參閱 Item[Int32]

IList.Remove(Object)

從集合中移除物件。

擴充方法

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱