Enable Disable Gridview checkbox based on condition in Asp.net CSharp.

satinder singh / Updated : / 3 Comments

 Disable Gridview Checkbox:  This article explains how to enable or disable gridview checkbox control based on data some condition in Asp.net c#. If you are looking for a way to make your Gridview checkbox enable or disable based on condition then you need to use GridView RowDataBound Event.

You can also check my previous article related to Gridview, or
Reorder Gridview row in Asp.net C# using jQuery ui Drag Drop , Sorting Gridview control on header click with pagination Asp.net.


How to Enable Disable Gridview checkbox based on the condition in Asp.net CSharp

Enable / Disable Gridview checkboxes based on the condition in Asp.net C#

Step to Enable / Disable Gridview Checkbox:

    1. Add Gridview Control and bind with data.
    2.  Add GridView RowDataBound Event and disable Checkboxes.

 # HTML Markup:

Let’s start by adding  a new page in our web application, and placed a Gridview control over it.

Here we bind data to our Asp.net gridview control which displays students academic scores. Inside TemplateField under ItemTemplate we added a Checkbox.

Now we want to disable Checkboxes based on condition. i.e Disable those checkboxes whose percentage less than 35 and Enable only those checkbox whose  percentage are greater than 35.

This is how our HTML looks like as shown below.

<asp:GridView ID="gvStudentGrade" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvStudentGrade_RowDataBound">
    <asp:TemplateField HeaderText="Action">
            <asp:CheckBox ID="chkBox" runat="server" />
    <asp:BoundField HeaderText="Sr no" DataField="srno" />
    <asp:BoundField HeaderText="Student Name" DataField="student_name" />
    <asp:TemplateField HeaderText="Percentage">
            <asp:TextBox ID="txtPercentage" Text='<%# Bind("percentage") %>' runat="server"></asp:TextBox>
    <asp:TemplateField HeaderText="Percentage">
            <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">


 # Code Behind:  Bind data to Asp.net Gridview Control.

Here on page load we call a method `bindGv_StudentGrade()` which basically get call when the page is not postback and bind data to our Gridview Control. In ‘bindGv_StudentGrade()’  method we created a DataTable and added some value to it.

Now we set gridview data source with newly created DataTable. By this, we are done with data binding to our gridview control. You can also bind with Database by calling SQL Query.

Finally, our code looks like as shown below.

protected void Page_Load(object sender, EventArgs e)
    if (!Page.IsPostBack) {


public void bindGv_StudentGrade()
    DataTable dtStudents = new DataTable();
    dtStudents.Columns.Add("srno", typeof(string));
    dtStudents.Columns.Add("student_name", typeof(string));
    dtStudents.Columns.Add("percentage", typeof(string));

    dtStudents.Rows.Add("1", "John Miller", "35");
    dtStudents.Rows.Add("2", "Amit Sarna", "85");
    dtStudents.Rows.Add("3", "David Macule", "75");
    dtStudents.Rows.Add("4", "Andrea ely", "60");
    dtStudents.Rows.Add("5", "Rohit Sharma", "20");
    dtStudents.Rows.Add("6", "Pamela Franz", "55");
    dtStudents.Rows.Add("7", "Leslie Mac", "92");

    gvStudentGrade.DataSource = dtStudents;


# Adding GridView RowDataBound Event:

Using GridView [highlight]RowDataBound[/highlight] Event, we can also allow or disallow editing in a GridView on a row-by-row basis, depending on the value of a field in that row. Before the GridView control can be rendered, each row in the control must be bound to a record in the data source.

The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs.

By using GridView RowDataBound Event, we can also show or hide button control in gridview, or any other Asp.net Control which placed inside Gridview control. We can also change Gridview Row background color based on the condition in Asp.net C#.

So finally our code looks like as shown below.

protected void gvStudentGrade_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
      if (e.Row.RowType == DataControlRowType.DataRow)
        TextBox txtPrc = (TextBox)e.Row.FindControl("txtPercentage");
        DropDownList ddl_1 = (DropDownList)e.Row.FindControl("DropDownList1");
        CheckBox chkBox = (CheckBox)e.Row.FindControl("chkBox");

        int percentage = Convert.ToInt32(txtPrc.Text);
          if (percentage< 35)
             txtPrc.Enabled = false;
             ddl_1.Enabled = false;
             chkBox.Enabled = false;


Output: Using GridView RowDataBound Event we are able to Enable / Disable all the controls inside Asp.net Gridview Control.

How to Enable Disable Gridview checkbox based on the condition in Asp.net CSharp

You must check these articles:

Hope you enjoyed this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time!

3 comments on “Enable Disable Gridview checkbox based on condition in Asp.net CSharp.”

  1. Nicholas Hain

    This is a really great article for people who are new to creating an asp:GridView that is editable for the first time. Thank you for sharing this with us! I found it to be extremely useful!

  2. mehmet

    Sir, İs there any complate sample that should be connect to a database like a SQL and also should contentent the insert, update ,delete and print

Post Comment

Your email address will not be published. Required fields are marked *