How to use CheckBoxField in GridView

GridView CheckBoxField
GridView is an ASP.NET server control. GridView allows 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 the data source.

GridView AutoGenerateColumns property allows us to create an AutoGenerateField object for each field in the data source. if we set the AutoGenerateColumns property value to True then each field is rendered as a column in GridView in the order that each field appears in the data source.

asp.net 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 of data control fields 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 has only two states True and False. CheckBoxFiled displays a checkbox for each item in the GridView server control. GridView control displays a CheckBoxFiled object as a column.

A checkbox is normally disabled (read-only) in GridView. But the 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 allows 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 the CheckBox from GridView in its insert mode by setting CheckBoxFiled's InsertVisible property.

Developers can display a caption in the CheckBoxFiled header section by setting the HeaderText property. HeaderImageURl property allows us to show an image in the CheckBoxFiled header.

The following ASP.NET C# example code demonstrates to us how can we use CheckBoxFiled in GridView server control.
GridViewCheckBoxFieldExample.aspx

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

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net GridView CheckBoxField example: how to use asp:CheckBoxField</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">GridView CheckBoxField Example</h2>
        <asp:Label ID="Label1" runat="server" Font-Italic="true" ForeColor="Red"></asp:Label>
        <asp:SqlDataSource 
            ID="SqlDataSource1"
            runat="server"
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT ProductID, ProductName, UnitPrice, Discontinued FROM Products"
            >
        </asp:SqlDataSource>
        <asp:GridView 
            ID="GridView1"
            runat="server"
            DataSourceID="SqlDataSource1"
            AllowPaging="true"
            ForeColor="AliceBlue"
            BackColor="DodgerBlue"
            BorderColor="LightSkyBlue"
            HeaderStyle-BackColor="DarkBlue"
            AutoGenerateColumns="false"
            >
            <Columns>
                <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" />
            </Columns>
        </asp:GridView>        
    </div>
    </form>
</body>
</html>