In this article I will explain how to use enable CheckBoxField column in GridView in ASP.Net with C# and VB.Net. CheckBoxField columns in ASP.Net GridView are disabled and can be modified only when the GridView Row is in Edit mode.
This article will explain how to programmatically enable the CheckBox in CheckBoxField column of ASP.Net GridView.
 
 
Database
I have made use of the following table Hobbies with the schema as follows.
Enable disabled CheckBoxField columns in GridView in ASP.Net
 
I have already inserted few records in the table
Enable disabled CheckBoxField columns in GridView in ASP.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
HTML Markup
The HTML Markup consists of an ASP.Net GridView with an OnRowDataBound event handler specified.
The first column in the GridView is a CheckBoxField which is bound to a Boolean or BIT column.
Note: You can only bind a Boolean or BIT Column to a CheckBoxField.
 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound = "OnRowDataBound">
    <Columns>
        <asp:CheckBoxField DataField="IsSelected" />
        <asp:BoundField DataField="HobbyId" HeaderText="Hobby Id" ItemStyle-Width="80" />
        <asp:BoundField DataField="Hobby" HeaderText="Hobby" ItemStyle-Width="150" />
    </Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
 
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
 
 
Populating the GridView control
The GridView is populated from the database inside the Page Load event of the page.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.BindGrid();
    }
}
 
private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT [HobbyId], [Hobby], [IsSelected] FROM Hobbies"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Me.BindGrid()
    End If
End Sub
 
Private Sub BindGrid()
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand("SELECT [HobbyId], [Hobby], [IsSelected] FROM Hobbies")
            Using sda As New SqlDataAdapter()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using dt As New DataTable()
                    sda.Fill(dt)
                    GridView1.DataSource = dt
                    GridView1.DataBind()
                End Using
            End Using
        End Using
    End Using
End Sub
 
The following screenshot displays the disabled CheckBoxField column in GridView
Enable disabled CheckBoxField columns in GridView in ASP.Net
 
 
Enabling CheckBoxField column in GridView using OnRowDataBound event
Inside the OnRowDataBound event handler, the CheckBox inside the CheckBoxField column is referenced and then its Enabled property is set to True.
C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        CheckBox checkBox = e.Row.Cells[0].Controls[0] as CheckBox;
        checkBox.Enabled = true;
    }
}
 
VB.Net
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim checkBox As CheckBox = TryCast(e.Row.Cells(0).Controls(0), CheckBox)
        checkBox.Enabled = True
    End If
End Sub
 
The following screenshot displays the enabled CheckBoxField column in GridView
Enable disabled CheckBoxField columns in GridView in ASP.Net
 
 
Demo
 
 
Downloads

Download Code