TableCellCollection.GetEnumerator 方法

定义

返回一个实现了 IEnumerator 的对象,该对象包含 TableCell 中的所有 TableCellCollection 对象。

public:
 virtual System::Collections::IEnumerator ^ GetEnumerator();
public System.Collections.IEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IEnumerator
override this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Function GetEnumerator () As IEnumerator

返回

一个实现了 IEnumerator 的对象,该对象包含 TableCell 内的所有 TableCellCollection 对象。

实现

示例

以下示例演示如何使用 GetEnumerator 方法创建 System.Collections.IEnumerator 实现的对象。 然后循环访问以显示所选行中的项。

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

    void Page_Load(Object sender, EventArgs e)
    {

        int numrows = 5;
        int numcells = 6;
        int counter = 1;
        ArrayList a_row = new ArrayList();

        // Create a table.
        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();
                rw.Cells.Add(cel);
                counter++;
            }
            Table1.Rows.Add(rw);
        }

        if (!IsPostBack)
        {

            // Create a DropDownList for the number of rows.
            for (int rowNum = 0; rowNum < numrows; rowNum++)
            {
                a_row.Add(rowNum.ToString());
            }

            List1.DataSource = a_row;
            List1.DataBind();

        }
    }

    void Button_Click(object sender, EventArgs e)
    {

        int rowNum = List1.SelectedIndex;
        TableCell current_cell;

        // Create the IEnumerator.
        IEnumerator myEnum = Table1.Rows[rowNum].Cells.GetEnumerator();

        Label1.Text = "The items in the selected row are: ";

        // Iterate through the IEnumerator and display its contents.
        while (myEnum.MoveNext())
        {
            current_cell = (TableCell)myEnum.Current;
            Label1.Text = Label1.Text + " " + current_cell.Text;
        }
    }

</script>

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

    <h3>TableCellCollection Example</h3>
    <asp:Table id="Table1" runat="server" />
    <br />&nbjsp;<br />
    <p style="text-align:center">
        Select a row:
        <br />&nbjsp;<br />
        Row: <asp:DropDownList id="List1" 
            runat="server"/>

        <br />&nbjsp;<br />
        <asp:Button id="Button1"
             Text="Create IEnumerator"
             OnClick="Button_Click"
             runat="server" />
        <br />&nbjsp;<br />
        <asp:Label id="Label1" runat="server"/>
    </p>

    </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">
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        
        Dim numRows As Integer = 5
        Dim numCells As Integer = 6
        Dim counter As Integer = 1
        Dim a_row As New ArrayList()
        
        ' Create a table.
        Dim rowNum As Integer
        For rowNum = 0 To numRows - 1
            Dim rw As New TableRow()
            Dim cellNum As Integer
            For cellNum = 0 To numCells - 1
                Dim cel As New TableCell()
                cel.Text = counter.ToString()
                rw.Cells.Add(cel)
                counter += 1
            Next
            Table1.Rows.Add(rw)
        Next
        
        If Not IsPostBack Then
            
            ' Create a DropDownList for the number of rows.
            For rowNum = 0 To numRows - 1
                a_row.Add(rowNum.ToString())
            Next
            
            List1.DataSource = a_row
            List1.DataBind()
        End If
    End Sub

    Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
        
        Dim row As Integer = List1.SelectedIndex
        Dim current_cell As TableCell
        
        ' Create the IEnumerator.
        Dim myEnum As IEnumerator = Table1.Rows(row).Cells.GetEnumerator()
        
        Label1.Text = "The items in the selected row are: "
        
        ' Iterate through the IEnumerator and display its contents.
        While myEnum.MoveNext()
            current_cell = CType(myEnum.Current, TableCell)
            Label1.Text = Label1.Text & " " & current_cell.Text
        End While
    End Sub

</script>

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

    <h3>TableCellCollection Example</h3>
    <asp:Table id="Table1" runat="server" />
    <br />&nbjsp;<br />
    <p style="text-align:center">
        Select a row:
        <br />&nbjsp;<br />
        Row: <asp:DropDownList id="List1" 
            runat="server"/>

        <br />&nbjsp;<br />
        <asp:Button id="Button1"
             Text="Create IEnumerator"
             OnClick="Button_Click"
             runat="server" />
        <br />&nbjsp;<br />
        <asp:Label id="Label1" runat="server"/>
    </p>

    </div>
    </form>
</body>
</html>

注解

使用此方法创建一个 System.Collections.IEnumerator 实现的对象,该对象可以轻松循环访问以获取 中的每个 TableCellCollection项。

IEnumerator.Current使用 属性获取集合中当前指向的项。

IEnumerator.MoveNext使用 方法移动到集合中的下一项。

IEnumerator.Reset使用 方法将枚举器移回初始位置。

注意

IEnumerator.MoveNext在创建System.Collections.IEnumerator实现的对象或使用 方法将枚举器移动到集合中的第一项之后IEnumerator.Reset,必须调用 方法。 否则,由 属性表示的 IEnumerator.Current 项未定义。

适用于

另请参阅