Export (0) Print
Expand All

GridViewCommandEventArgs.CommandSource Property

Gets the source of the command.

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

public Object CommandSource { get; }

Property Value

Type: System.Object
A instance of the System.Object class that represents the source of the command.

Use the CommandSource property to access the properties of the object that represents the source of the command. In the case of a GridViewCommandEventArgs object, the source of the command is the GridView control that contains the button clicked by the user.

The following example demonstrates how to use the CommandSource property to access the GridView control that contains the button clicked by the user.

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

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

  void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
    // If multiple ButtonField columns are used, use the 
    // CommandName property to determine which button was clicked. 
      // Convert the row index stored in the CommandArgument 
      // property to an Integer. 
      int index = Convert.ToInt32(e.CommandArgument);

      // Retrieve the row that contains the button clicked 
      // by the user from the Rows collection. Use the 
      // CommandSource property to access the GridView control.
      GridView customersGridView = (GridView)e.CommandSource;
      GridViewRow row = customersGridView.Rows[index];

      // Create a new ListItem object for the customer in the row.
      ListItem item = new ListItem();
      item.Text = Server.HtmlDecode(row.Cells[1].Text) + " " + Server.HtmlDecode(row.Cells[2].Text);

      // If the author is not already in the ListBox, add the ListItem 
      // object to the Items collection of a ListBox control. 

  void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)

    // The GridViewCommandEventArgs class does not contain a 
    // property that indicates which row's command button was 
    // clicked. To identify which row was clicked, use the button's 
    // CommandArgument property by setting it to the row's index. 
    if(e.Row.RowType == DataControlRowType.DataRow)
      // Retrieve the LinkButton control from the first column.
      LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];

      // Set the LinkButton's CommandArgument property with the 
      // row's index.
      addButton.CommandArgument = e.Row.RowIndex.ToString();



<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridViewCommandEventArgs Example</title>
    <form id="form1" runat="server">

      <h3>GridViewCommandEventArgs Example</h3>

      <table width="100%">
          <td style="width:50%">
            <asp:gridview id="CustomersGridView" 


                <asp:buttonfield buttontype="Link" commandname="Add" text="Add"/>
                <asp:boundfield datafield="CompanyName" 
                  headertext="Company Name"/>
                <asp:boundfield datafield="City" 



          <td style="vertical-align:top; width:50%">
             Customers: <br/>
             <asp:listbox id="CustomersListBox"

      <!-- 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="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

© 2014 Microsoft