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

/ / 3 Comments

[ads] 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.

Screenshot:

[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 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">
<Columns>
    <asp:TemplateField HeaderText="Action">
        <ItemTemplate>
            <asp:CheckBox ID="chkBox" runat="server" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField HeaderText="Sr no" DataField="srno" />
    <asp:BoundField HeaderText="Student Name" DataField="student_name" />
    <asp:TemplateField HeaderText="Percentage">
        <ItemTemplate>
            <asp:TextBox ID="txtPercentage" Text='<%# Bind("percentage") %>' runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Percentage">
        <ItemTemplate>
            <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
                <asp:ListItem>One</asp:ListItem>
                <asp:ListItem>two</asp:ListItem>
                <asp:ListItem>three</asp:ListItem>
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>
 

 # 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) {
        bindGv_StudentGrade();
    }
}
//*
 
//*
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;
    gvStudentGrade.DataBind();
}
//*
[ads_468_60]

# 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!

[txt_ads]

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 do a favor, then 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 *

3 Comments

  1. mehmet 04/25/2016 07:38:25
    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
  2. Satinder singh 04/26/2016 05:51:24
    Hi Mehmet, Yes you can check this https://codepedia.info/2012/08/how-to-add-update-record-using-gridview-control-c/ where have explain how to Insert, Update, Delete records in Gridview with live Database [MS SQL]
  3. Nicholas Hain 11/09/2018 17:34:43
    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!