Enable/Disable CheckBox Inside the GridView Control

Introduction

In this article you will see how to enable or disable a checkbox in GridView records based on the condition.

RowDataBound Event

A GridviewRowEventArgs object is passed to the event-handling method, that enables you to access the properties of the row being bound. To access a specific cell in the row, use the "cell" property of the GridViewRow object contained in the Row property of the GridEventRowEventArgs object.

Now let's have a look at this web application to see how to use the RowDataBound Event.

Step 1 :

Open Visual Studio then select "File" -> "New" -> "Website..." as in the following figure:

NewWebApp

Step 2 :

Now go to the Solution Explorer and add a new item as in the following figure:

NewItem

Step 3 :

Now add a new web form on your web application as in the following figure:

AddWebForm

Step 4 :

Write the following code in the "default.aspx" page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Enable/Disable Checkbox in Gridview based on condtion in ASP.Net</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:GridView ID="gvDetails" AutoGenerateColumns="False" CellPadding="3" runat="server"

OnRowDataBound="gvDetails_RowDataBound" BackColor="White" 

BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" ForeColor="Black" GridLines="Vertical">

    <AlternatingRowStyle BackColor="#CCCCCC" />

<Columns>

<asp:TemplateField>

<ItemTemplate>

<asp:CheckBox ID="chkSelect" runat="server" />

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField HeaderText="UserId" DataField="UserId" />

<asp:BoundField HeaderText="UserName" DataField="UserName" />

<asp:BoundField HeaderText="Education" DataField="Education" />

<asp:BoundField HeaderText="Location" DataField="Location" />

</Columns>

    <FooterStyle BackColor="#CCCCCC" />

<HeaderStyle BackColor="Black" Font-Bold="true" ForeColor="White" />

    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />

    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />

    <SortedAscendingCellStyle BackColor="#F1F1F1" />

    <SortedAscendingHeaderStyle BackColor="#808080" />

    <SortedDescendingCellStyle BackColor="#CAC9C9" />

    <SortedDescendingHeaderStyle BackColor="#383838" />

</asp:GridView>

</div>

</form>

</body>

</html>

 

Add the connectionstring in web.config file such like :

<connectionStrings>

 <add name="dbconnection" connectionString="Data Source=; Initial Catalog=Employee; 

User=abc; Password=***" providerName="SqlClient"/>

</connectionStrings>

 

Step 5 :

Now write the following code in the "default.aspx.cs" page:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Data;

using System.Web.UI.WebControls;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            BindData();

        }

    }

    protected void BindData()

    {

        DataTable dt = new DataTable();

        dt.Columns.Add("UserId"typeof(Int32));

        dt.Columns.Add("UserName"typeof(string));

        dt.Columns.Add("Education"typeof(string));

        dt.Columns.Add("Location"typeof(string));

        DataRow dtr = dt.NewRow();

        dtr["UserId"] = 1;

        dtr["UserName"] = "Pankaj Lohani";

        dtr["Education"] = "MCA";

        dtr["Location"] = "New Delhi";

        dt.Rows.Add(dtr);

        dtr = dt.NewRow();

        dtr["UserId"] = 2;

        dtr["UserName"] = "Nimit Joshi";

        dtr["Education"] = "MCA";

        dtr["Location"] = "Ghaziabad";

        dt.Rows.Add(dtr);

        dtr = dt.NewRow();

        dtr["UserId"] = 3;

        dtr["UserName"] = "Amit Senwal";

        dtr["Education"] = "BCA";

        dtr["Location"] = "Greater Noida";

        dt.Rows.Add(dtr);

        dtr = dt.NewRow();

        dtr["UserId"] = 4;

        dtr["UserName"] = "Pravesh Khanduri";

        dtr["Education"] = "BCA";

        dtr["Location"] = "Delhi";

        dt.Rows.Add(dtr);

        dtr = dt.NewRow();

        dtr["UserId"] = 5;

        dtr["UserName"] = "Ravi Kumar";

        dtr["Education"] = "BCA";

        dtr["Location"] = "Delhi";

        dt.Rows.Add(dtr);

        dtr = dt.NewRow();

        dtr["UserId"] = 6;

        dtr["UserName"] = "Ainul Hasan";

        dtr["Education"] = "MCA";

        dtr["Location"] = "Gurgaon";

        dt.Rows.Add(dtr);

        gvDetails.DataSource = dt;

        gvDetails.DataBind();

    }

    protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            CheckBox chkbox = (CheckBox)e.Row.FindControl("chkSelect");

            if (e.Row.Cells[3].Text == "BCA")

            {

                chkbox.Enabled = false;

            }

            else

            {

                chkbox.Enabled = true;

            }

        }

    }

}

 

In the code above you saw that before the GridView control can be rendered, each row in the control must be bound to a record. The "RowDataBound" event is raised when a data row is bound to data in the GridView control. This enables you to provide an event-handling method that performs a modification of the values bound to the row, when the event occurs.

Step 6 :

Now debug the application by pressing F5; the output will then appear in the browser as in the following figure:

Debug

Step 7 :

Now you can check only the enable checkbox as in the following figure:

EnableCheckbox

Summary

ASP.NET automatically remembers whether the Checkboxes were and were not checked across postbacks. We can also programmatically access the checkbox in code to determine whether a given checkbox is checked, or to change the checked state.