Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
CopyTo Method

TableRowCollection.CopyTo Method

Copies the items from the TableRowCollection to the specified System.Array, starting with the specified index in the System.Array.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public void CopyTo(
	Array array,
	int index


Type: System.Array

A zero-based System.Array that receives the copied items from the TableRowCollection.

Type: System.Int32

The first position in the specified System.Array to receive copied contents.


ICollection.CopyTo(Array, Int32)

Use this method to copy the contents of the TableRowCollection into the specified System.Array, starting at the specified index.


The array parameter must be a zero-based System.Array.

The following example demonstrates how to use the CopyTo method to copy the items from the TableRowCollection into the specified array.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

<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();

    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();

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>TableCellCollection Example</title>
    <form id="form1" runat="server">
        <h3>TableCellCollection Example</h3>
        <asp:Table id="Table1" runat="server"/>
        <br />&nbsp;<br />
        <asp:Button id="Button1"
            Text="Copy Table to Array"
        <br />&nbsp;<br />
        <asp:Label id="Label1" runat="server"/>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
© 2015 Microsoft