DataKeyArray.Count Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets the number of items in the collection.

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

public int Count { get; }

Property Value

Type: System.Int32

The number of items in the collection.

Use the Count property to determine the number of items in the collection. The Count property is commonly used when iterating through the collection to determine its upper bound. The Count property is also commonly used to determine whether a collection is empty.

The following code example demonstrates how to use the Count property to determine whether a DataKeyArray collection is empty.

<%@ 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 Count property to determine whether the
    // DataKeys collection contains any items.
    if (CustomerGridView.DataKeys.Count > 0)

      MessageLabel.Text = "The primary key of each record displayed are: <br/><br/>";

      // Use the GetEnumerator method to create an enumerator that 
      // contains the DataKey objects for the GridView control.
      IEnumerator keyEnumerator = CustomerGridView.DataKeys.GetEnumerator();

      // Iterate though the enumerator and display the primary key
      // value of each record displayed.
      while (keyEnumerator.MoveNext())
        DataKey key = (DataKey)keyEnumerator.Current;
        MessageLabel.Text += key.Value.ToString() + "<br/>";

      MessageLabel.Text = "No DataKey objects.";



<html xmlns="" >

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


.NET Framework
Available since 2.0
Return to top