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

2
Our Reader Score
[Total: 0 Average: 0]

 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:

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.

 


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

 


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

 

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!


Get your Printable Copy

2 COMMENTS

  1. 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

LEAVE A REPLY