TableCellCollection Class

Definition

封装 TableHeaderCellTableCell 对象的集合,这些对象组成了 Table 控件中的一行。Encapsulates a collection of TableHeaderCell and TableCell objects that make up a row in a Table control. 此类不能被继承。This class cannot be inherited.

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
Inheritance
TableCellCollection
Implements

Examples

下面的示例演示如何以编程方式填充 Table 控件。The following example demonstrates how to programmatically fill a Table control. TableCell 对象(表示单个单元格)通过 Cells 属性添加到 TableRow 对象,这些对象表示各个行。TableCell objects, which represent individual cells, are added to TableRow objects, which represent the individual rows, through the Cells property.

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

Remarks

使用此类以编程方式管理 TableCell 对象的集合,这些对象构成 Table 控件中的行。Use this class to programmatically manage a collection of TableCell objects that make up a row in a Table control. 此类通常用于在 Table 控件中的行中添加或删除单元。This class is commonly used to add or remove cells from a row in a Table control.

Note

Table 控件包含一个 Rows 集合,该集合表示 TableRow 对象的集合。A Table control contains a Rows collection that represents a collection of TableRow objects. 每个 TableRow 都表示表中的单个行并包含一个表示 TableCell 对象集合的 Cells 集合。Each TableRow represents an individual row in the table and contains a Cells collection that represents a collection of TableCell objects. 这些 TableCell 对象表示表中的单个单元格。These TableCell objects represent the individual cells in the table. 若要获取单个单元,必须首先从 Table 控件的 Rows 集合获取 TableRowTo get an individual cell, you must first get a TableRow from the Rows collection of a Table control. 然后,你可以从 TableRowCells 集合获取 TableCellYou can then get a TableCell from the Cells collection of the TableRow.

Properties

Count

获取 TableCell 中的 TableCellCollection 对象数。Gets the number of TableCell objects in the TableCellCollection.

IsReadOnly

获取一个值,该值指示 TableCellCollection 是否为只读。Gets a value indicating whether the TableCellCollection is read-only.

IsSynchronized

获取一个值,该值指示是否同步对 TableCellCollection 的访问(线程安全)。Gets a value indicating whether access to the TableCellCollection is synchronized (thread-safe).

Item[Int32]

TableCell 中获取位于指定索引位置的 TableCellCollectionGets a TableCell from the TableCellCollection at the specified index.

SyncRoot

获取可用于同步 TableCellCollection 访问的对象。Gets the object that can be used to synchronize access to the TableCellCollection.

Methods

Add(TableCell)

将指定的 TableCell 追加到 TableCellCollection 的末尾。Appends the specified TableCell to the end of the TableCellCollection.

AddAt(Int32, TableCell)

将指定的 TableCell 添加到 TableCellCollection 中的指定索引位置。Adds the specified TableCell to the TableCellCollection at the specified index location.

AddRange(TableCell[])

TableCell 对象从指定的数组追加到集合的末尾。Appends the TableCell objects from the specified array to the end of the collection.

Clear()

TableCell 中移除所有 TableCellCollection 对象。Removes all TableCell objects from the TableCellCollection.

CopyTo(Array, Int32)

TableCellCollection 中的指定索引开始,将 Array 中的项复制到指定的 ArrayCopies the items from the TableCellCollection to the specified Array, starting with the specified index in the Array.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetCellIndex(TableCell)

返回一个值,该值表示 TableCell 中指定 TableCellCollection 的索引。Returns a value that represents the index of the specified TableCell from the TableCellCollection.

GetEnumerator()

返回一个实现了 IEnumerator 的对象,该对象包含 TableCell 中的所有 TableCellCollection 对象。Returns a IEnumerator implemented object that contains all TableCell objects in the TableCellCollection.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(TableCell)

TableCell 中移除指定的 TableCellCollectionRemoves the specified TableCell from the TableCellCollection.

RemoveAt(Int32)

TableCell 中移除位于指定索引位置的 TableCellCollectionRemoves a TableCell from the TableCellCollection at the specified index.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

IList.Add(Object)

将对象添加到集合。Adds an object to the collection.

IList.Contains(Object)

确定指定对象是否位于集合内。Determines whether the specified object is contained within the collection.

IList.IndexOf(Object)

搜索指定的对象,并返回集合中第一个匹配项的从零开始的索引。Searches for the specified object and returns the zero-based index of the first occurrence within the collection.

IList.Insert(Int32, Object)

将对象插入到集合中的指定索引处。Inserts an object into the collection at the specified index.

IList.IsFixedSize

有关此成员的说明,请参见 IsFixedSizeFor a description of this member, see IsFixedSize.

IList.Item[Int32]

有关此成员的说明,请参见 Item[Int32]For a description of this member, see Item[Int32].

IList.Remove(Object)

从集合中移除一个对象。Removes an object from the collection.

Extension Methods

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

Applies to

See also