How to use CheckBoxField in GridView

GridView CheckBoxField
GridView is an server control. GridView allow us to display data source values in a table. within a gridview control each column represents a field and each row represents a record of data source.

GridView AutoGenerateColumns property allow us to create an AutoGenerateField object for each field in the data source. if we set the AutoGenerateColumns property value to True then each filed rendered as a column in gridview in the order that each field appears in the data source. developers can manually specify which column fields appear in gridview by setting the AutoGenerateColumns property value to False. if the gridview AutoGenerateColumns property value is false, we can define our own column field collection and column filed types.

each column in a gridview is represented by a DataControlField object. data control fields are used by gridview to represent a field of data. different types data control field are BoundField, ButtonField, CheckBoxField, CommandField, HyperLinkField, ImageField and TemplateField.

CheckBoxField column field type is commonly used to display fields with a Boolean value because a Boolean have only two states True and False. CheckBoxFiled display a checkbox for each item in the gridview server control. gridview control display a CheckBoxFiled object as a column.

a checkbox is normally disbled (readonly) in gridview. but check box is enabled in gridview's edit mode. we can specify the data source field to display as CheckBoxFiled in gridview by setting the CheckBoxField's DataField property.

CheckBoxFiled's Text property allow us to display a caption next to each checkbox in gridview. we can hide checkboxes from gridview by setting the CheckBoxField's Visible property. developers also can hide checkbox from gridview in its insert mode by setting CheckBoxFiled's InsertVisible property.

developers can display a caption in CheckBoxFiled header section by setting HeaderText property. HeaderImageURl property allow us to show an image in CheckBoxFiled header.

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

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">


<html xmlns="">
<head id="Head1" runat="server">
    <title> GridView CheckBoxField example: how to use asp:CheckBoxField</title>
    <form id="form1" runat="server">
        <h2 style="color:Navy">GridView CheckBoxField Example</h2>
        <asp:Label ID="Label1" runat="server" Font-Italic="true" ForeColor="Red"></asp:Label>
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT ProductID, ProductName, UnitPrice, Discontinued FROM Products"
                <asp:BoundField HeaderText="Product ID" DataField="ProductID" />
                <asp:BoundField HeaderText="Product Name" DataField="ProductName" />
                <asp:BoundField HeaderText="Unit Price" DataField="UnitPrice" />
                <asp:CheckBoxField HeaderText="Discontinued" DataField="Discontinued" />
More examples