Export (0) Print
Expand All

DataKeyArray.CopyTo Method

Copies all the items from this collection to the specified array of DataKey objects, starting at the specified index in the array.

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

public void CopyTo(
	DataKey[] array,
	int index


Type: System.Web.UI.WebControls.DataKey[]

A zero-based array of DataKey objects that receives the copied items from the collection.

Type: System.Int32

The first index in the specified array to receive the copied contents.

Use the CopyTo method to copy the items from the collection into the specified array, starting at the specified index. The array can then be used to access the items in the collection.


You must use a zero-based array for the array parameter.

The following code example demonstrates how to use the CopyTo method to copy the DataKey objects from a DataKeyArray collection to an array of DataKey objects.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  void CustomerGridView_DataBound(Object sender, EventArgs e)
    // Use the indexer to retrieve the DataKey object for the 
    // first record.
    DataKey key = CustomerGridView.DataKeys[0];

    // Display the the value of the primary key for the first
    // record displayed in the GridView control.
    MessageLabel.Text = "The primary key of the first record displayed is " +
      key.Value.ToString() + ".";

  void CopyArray_Click(Object sender, EventArgs e)
      DataKeyArray theKeys = CustomerGridView.DataKeys;
      DataKey[] myNewArray = new DataKey[theKeys.Count];
      theKeys.CopyTo(myNewArray, 0);
      Label2.Visible = true;

      // Display first page key values from the new array.
      for (int i = 0; i < myNewArray.Length; i++)
          Label2.Text += "<br />" + myNewArray[i].Value;



<html  >

  <head id="Head1" runat="server">
    <title>DataKeyArray Example</title>
    <form id="form1" runat="server">

      <h3>DataKeyArray Example</h3>

        <asp:gridview id="CustomerGridView"



        <asp:label id="MessageLabel"

        <!-- This example uses Microsoft SQL Server and connects  -->
        <!-- to the Northwind sample database. Use an ASP.NET     -->
        <!-- expression to retrieve the connection string value   -->
        <!-- from the Web.config file.                            -->
        <asp:sqldatasource id="CustomerDataSource"
          selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
          connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>" 

        <asp:Button ID="CopyArray" 
            Text="Copy DataKeyArray to Array" 
            OnClick="CopyArray_Click" />

        <br />

        <asp:label id="Label2"
          Text="First page of Copied Array Key Values" />


.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft