How to use CommandField in GridView

GridView CommandField
GridView is an web server control. developers can manually specify which field appear in gridview by setting the AutoGenerateColumns property value to False. they can define their own column field collection and column field types. gridview supports different column field types, these are BoundField, ButtonField, CheckBoxField, CommandField, HyperLinkField, ImageField nad TemplateField.

CommandField represents a special field that displays command buttons to perform selecting, editing or deleting operations in gridview. gridview server control display a CommandField as a column.

CommandField's ShowSelectButton property allow us to show or hide a Select button in a CommandField field for each record in gridview. this Select button allow us to select a record in the gridview server control.

ShowEditButton property provide a way to show or hide an Edit button in a CommandField field for each record in gridview. this Edit button allow us to edit a record from data source. if we click an Edit button for a specific record, the edit button is replaced with an Update button and a Cancel button, all other Command buttons are also hidden.

ShowDeleteButton property allow us to show or hide a Delete button in a CommandField field for each record in gridview. this Delete button allow us to delete a record from the data source.

SelectImageUrl, EditImageUrl, DeleteImageUrl, UpdateImageUrl and CancelImageUrl properties allow us to display image as button instead Select, Edit, Delete, Update and Cancel default button. to apply a image button instead default command button we also need to set the ByttonType property value to Image.

developers can change the default command button text by setting the following properties those are SelectText, EditText, DeleteText, CancelText and UpdateText property.

the following c# example code demonstrate us how can we use CommandField in gridview server control.

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

<!DOCTYPE html>

<script runat="server">


<html xmlns="">
<head id="Head1" runat="server">
    <title> GridView CommandField, SelectedRowStyle example: how to use CommandField</title>
    <form id="form1" runat="server">
        <h2 style="color:Navy">GridView CommandField, SelectedRowStyle Example</h2>
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT CategoryID, CategoryName, Description FROM Categories"
                <asp:BoundField HeaderText="Category ID" DataField="CategoryID" />
                <asp:BoundField HeaderText="Category Name" DataField="CategoryName" />
                <asp:BoundField HeaderText="Description" DataField="Description" />
                <asp:CommandField ShowSelectButton="true" ButtonType="Link" SelectText="Select" />
            <SelectedRowStyle BackColor="LightPink" Font-Italic="true" ForeColor="Crimson" />
More examples