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 Value

Type: System.Collections.Specialized.IOrderedDictionary
A dictionary that contains 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 get the value of the primary key or keys of the row to delete.


To get 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 in the Keys dictionary. To determine the value of a key field, use the DictionaryEntry.Value property.

The following example shows how to use the Values property to get the values of the key fields for 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%>"


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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