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


Disable Gridview Checkbox:  This article explains how to enable or disable gridview checkbox control based on data some conditions 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.


[caption id="attachment_2958" align="aligncenter" width="574"]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#[/caption]

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 student's 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 is less than 35 and Enable only those checkboxes whose percentage is 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 gets called 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 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:

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to thank me? 👳 Buy Me a Coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.

Post Comment

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