次の方法で共有


TableRowCollection.CopyTo メソッド

指定した System.ArrayTableRowCollection の項目をコピーします。コピー操作は、System.Array 内の指定したインデックス位置から始まります。

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)

構文

'宣言
Public Sub CopyTo ( _
    array As Array, _
    index As Integer _
)
'使用
Dim instance As TableRowCollection
Dim array As Array
Dim index As Integer

instance.CopyTo(array, index)
public void CopyTo (
    Array array,
    int index
)
public:
virtual void CopyTo (
    Array^ array, 
    int index
) sealed
public final void CopyTo (
    Array array, 
    int index
)
public final function CopyTo (
    array : Array, 
    index : int
)
適用できません。

パラメータ

  • index
    指定した System.Array 内の、内容のコピー開始位置。

解説

このメソッドを使用して、指定した System.ArrayTableRowCollection の内容をコピーします。コピー操作は、指定したインデックス位置から始まります。

メモメモ :

array パラメータには、インデックス番号が 0 から始まる System.Array を指定する必要があります。

使用例

CopyTo メソッドを使用して、指定した配列に TableRowCollection の項目をコピーする方法の例を次に示します。

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Text" %>

<!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
    End Sub

    Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim rowCounter As Integer = 0
        Dim myRowArray(4) As TableRow
        Dim myCellArray(5) As TableCell
        Dim tb As New StringBuilder

        ' Copy the Rows collection to an array.
        Table1.Rows.CopyTo(myRowArray, 0)

        tb.Append("The copied items from the table are: <br />")

        ' Iterate through the TableRows in the array.
        Dim rw As TableRow
        For Each rw In myRowArray
            ' Copy the Cells collection of a row to an array.
            Table1.Rows(rowCounter).Cells.CopyTo(myCellArray, 0)

            ' Iterate through the cell array 
            ' and display its contents.
            Dim cell As TableCell
            For Each cell In myCellArray
                tb.Append(cell.Text & ", ")
            Next
            Label1.Text = tb.ToString()

            rowCounter += 1
        Next
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" 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 />&nbsp;<br />
        <asp:Button id="Button1"
            Text="Copy Table to Array"
            OnClick="Button_Click"
            runat="server"/>
        <br />&nbsp;<br />
        <asp:Label id="Label1" runat="server" />
    </div>
    </form>
</body>
</html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Text" %>

<!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();
            int cellNum = 0;
            for (cellNum = 0; cellNum < numCells; cellNum++)
            {
                TableCell cel = new TableCell();
                cel.Text = counter.ToString();
                rw.Cells.Add(cel);
                counter++;
            }
            Table1.Rows.Add(rw);
        }
    }

    void Button_Click(object sender, EventArgs e)
    {
        int rowCounter = 0;
        TableRow[] myRowArray = null;
        TableCell[] myCellArray = null;
        StringBuilder tb = new StringBuilder();

        // Copy the Rows collection to an array.
        Table1.Rows.CopyTo(myRowArray, 0);

        tb.Append("The copied items from the table are: \n");

        // Iterate through the TableRows in the array.
        foreach (TableRow rw in myRowArray)
        {
            // Copy the Cells collection of a row to an array.
            Table1.Rows[rowCounter].Cells.CopyTo(myCellArray, 0);

            // Iterate through the cell array 
            // and display its contents.
            foreach (TableCell cell in myCellArray)
                tb.Append(cell.Text + ", ");

            Label1.Text = tb.ToString();
            rowCounter++;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" 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 />&nbsp;<br />
        <asp:Button id="Button1"
            Text="Copy Table to Array"
            OnClick="Button_Click"
            runat="server"/>
        <br />&nbsp;<br />
        <asp:Label id="Label1" runat="server"/>
    </div>
    </form>
</body>
</html>

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

TableRowCollection クラス
TableRowCollection メンバ
System.Web.UI.WebControls 名前空間
System.Array

その他の技術情報

Table、TableRow、TableCell の各 Web サーバー コントロール