This documentation is archived and is not being maintained.

GridViewDeleteEventArgs.Keys Property

Gets a dictionary of field name/value pairs that represent the primary key of the row to delete.

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

public IOrderedDictionary Keys { get; }
/** @property */
public IOrderedDictionary get_Keys ()

public function get Keys () : IOrderedDictionary

Not applicable.

Property Value

A System.Collections.Specialized.IOrderedDictionary object containing field name/value pairs that represent the primary key of the row to delete.

When the DataKeyNames property of a GridView control is set, use the Keys property (dictionary) to access the value of the primary key or keys in the row to delete.


To access the values of the non-key fields, use the Values property.

The Keys dictionary is automatically populated with the name/value pairs of the field or fields specified in the DataKeyNames property. If multiple fields form the primary key, a separate entry is added to the Keys dictionary for each key field.

To determine the name of a key field, use the DictionaryEntry.Key property of a System.Collections.DictionaryEntry object contained in the Keys dictionary. To determine the value of a key field, use the DictionaryEntry.Value property.

The following example demonstrates how to use the Values property to access the values of the key fields in the row to delete. The values are then written to a log file of deleted records.

<%@ Page language="C#" %>
<%@ import namespace="System.IO" %>

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

  void CustomersGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)

    // Record the delete operation in a log file.

    // Create the log text. 
    String logText = "";

    // Append the values of the key fields to the log text.
    foreach (DictionaryEntry keyEntry in e.Keys)
      logText += keyEntry.Key + "=" + keyEntry.Value + ";";

    // Append the values of the non-key fields to the log text.
    foreach (DictionaryEntry valueEntry in e.Values)
      logText += valueEntry.Key + "=" + valueEntry.Value + ";";

    // Display the log content.
    LogTextLabel.Text = logText;
    // Append the text to a log file.
      StreamWriter sw;
      sw = File.AppendText(Server.MapPath(null) + "\\deletelog.txt");
    catch(UnauthorizedAccessException ex)
      // You must provide read/write access to the file using ACLs.
      LogErrorLabel.Text = "You do not have permission to write to the log.";

  void CustomersGridView_RowDeleted(Object sender, GridViewDeletedEventArgs e)
    if (e.Exception == null)
      // The delete operation succeeded. Clear the message label.
      Message.Text = "";
      // The delete operation failed. Display an error message.
      Message.Text = e.AffectedRows.ToString() + " rows deleted. " + e.Exception.Message;
      e.ExceptionHandled = true;


<html xmlns="" >
  <head runat="server">
    <title>GridViewDeleteEventArgs Keys and Values Example</title>
    <form id="form1" runat="server">
      <h3>GridViewDeleteEventArgs Keys and Values Example</h3>
      <asp:label id="Message"
      <asp:label id="LogTextLabel"
      <asp:label id="LogErrorLabel"

      <asp:gridview id="CustomersGridView" 
      <!-- 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="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0