DataKeyArray.CopyTo(DataKey[], Int32) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将此集合中的所有项复制到 DataKey 对象的指定数组(从数组中的指定索引位置开始)。
public:
void CopyTo(cli::array <System::Web::UI::WebControls::DataKey ^> ^ array, int index);
public void CopyTo (System.Web.UI.WebControls.DataKey[] array, int index);
member this.CopyTo : System.Web.UI.WebControls.DataKey[] * int -> unit
Public Sub CopyTo (array As DataKey(), index As Integer)
参数
- index
- Int32
指定的数组中接收复制内容的第一个索引。
示例
下面的代码示例演示如何使用CopyTo该方法将对象从DataKeyArray集合复制到DataKey对象数组DataKey。
<%@ 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 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 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;
}
}
</script>
<html >
<head id="Head1" runat="server">
<title>DataKeyArray Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataKeyArray Example</h3>
<asp:gridview id="CustomerGridView"
datasourceid="CustomerDataSource"
autogeneratecolumns="true"
datakeynames="CustomerID"
allowpaging="true"
ondatabound="CustomerGridView_DataBound"
runat="server">
</asp:gridview>
<br/>
<asp:label id="MessageLabel"
forecolor="Red"
runat="server"/>
<!-- 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 %>"
runat="server"/>
<asp:Button ID="CopyArray"
runat="server"
Text="Copy DataKeyArray to Array"
OnClick="CopyArray_Click" />
<br />
<asp:label id="Label2"
runat="server"
Visible="false"
Text="First page of Copied Array Key Values" />
</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 CustomerGridView_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles CustomerGridView.DataBound
' Use the indexer to retrieve the DataKey object for the
' first record.
Dim key As DataKey = CustomerGridView.DataKeys(0)
' Display 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() & "."
End Sub
Sub CopyArray_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim theKeys As DataKeyArray = CustomerGridView.DataKeys
Dim myNewArray(theKeys.Count - 1) As DataKey
theKeys.CopyTo(myNewArray, 0)
Label2.Visible = True
' Display first page key values from the new array.
For i As Integer = 0 To myNewArray.Length - 1
Label2.Text &= "<br />" & myNewArray(i).Value
Next i
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>DataKeyArray Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataKeyArray Example</h3>
<asp:gridview id="CustomerGridView"
datasourceid="CustomerDataSource"
autogeneratecolumns="true"
datakeynames="CustomerID"
allowpaging="true"
runat="server">
</asp:gridview>
<br/>
<asp:label id="MessageLabel"
forecolor="Red"
runat="server"/>
<!-- 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%>"
runat="server"/>
<asp:Button ID="CopyArray"
runat="server"
Text="Copy DataKeyArray to Array"
OnClick="CopyArray_Click" />
<br />
<asp:label id="Label2"
runat="server"
Visible="false"
Text="First page of Copied Array Key Values" />
</form>
</body>
</html>
注解
使用该方法 CopyTo 将集合中的项复制到指定数组中,从指定的索引开始。 然后,该数组可用于访问集合中的项。
备注
必须为参数使用从零开始的 array
数组。