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:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>Enable/Disable Checkbox in Gridview based on condtion in ASP.Net</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <asp:GridView ID="gvDetails" AutoGenerateColumns="False" CellPadding="3" runat="server"
  11. OnRowDataBound="gvDetails_RowDataBound" BackColor="White"
  12. BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" ForeColor="Black" GridLines="Vertical">
  13. <AlternatingRowStyle BackColor="#CCCCCC" />
  14. <Columns>
  15. <asp:TemplateField>
  16. <ItemTemplate>
  17. <asp:CheckBox ID="chkSelect" runat="server" />
  18. </ItemTemplate>
  19. </asp:TemplateField>
  20. <asp:BoundField HeaderText="UserId" DataField="UserId" />
  21. <asp:BoundField HeaderText="UserName" DataField="UserName" />
  22. <asp:BoundField HeaderText="Education" DataField="Education" />
  23. <asp:BoundField HeaderText="Location" DataField="Location" />
  24. </Columns>
  25. <FooterStyle BackColor="#CCCCCC" />
  26. <HeaderStyle BackColor="Black" Font-Bold="true" ForeColor="White" />
  27. <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
  28. <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
  29. <SortedAscendingCellStyle BackColor="#F1F1F1" />
  30. <SortedAscendingHeaderStyle BackColor="#808080" />
  31. <SortedDescendingCellStyle BackColor="#CAC9C9" />
  32. <SortedDescendingHeaderStyle BackColor="#383838" />
  33. </asp:GridView>
  34. </div>
  35. </form>
  36. </body>
  37. </html>

Add the connectionstring in web.config file such like :

  1. <connectionStrings>
  2. <add name="dbconnection" connectionString="Data Source=; Initial Catalog=Employee;
  3. User=abc; Password=***" providerName="SqlClient"/>
  4. </connectionStrings>

Step 5

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Data;
  7. using System.Web.UI.WebControls;
  8. public partial class _Default : System.Web.UI.Page
  9. {
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. if (!IsPostBack)
  13. {
  14. BindData();
  15. }
  16. }
  17. protected void BindData()
  18. {
  19. DataTable dt = new DataTable();
  20. dt.Columns.Add("UserId", typeof(Int32));
  21. dt.Columns.Add("UserName", typeof(string));
  22. dt.Columns.Add("Education", typeof(string));
  23. dt.Columns.Add("Location", typeof(string));
  24. DataRow dtr = dt.NewRow();
  25. dtr["UserId"] = 1;
  26. dtr["UserName"] = "Pankaj Lohani";
  27. dtr["Education"] = "MCA";
  28. dtr["Location"] = "New Delhi";
  29. dt.Rows.Add(dtr);
  30. dtr = dt.NewRow();
  31. dtr["UserId"] = 2;
  32. dtr["UserName"] = "Nimit Joshi";
  33. dtr["Education"] = "MCA";
  34. dtr["Location"] = "Ghaziabad";
  35. dt.Rows.Add(dtr);
  36. dtr = dt.NewRow();
  37. dtr["UserId"] = 3;
  38. dtr["UserName"] = "Amit Senwal";
  39. dtr["Education"] = "BCA";
  40. dtr["Location"] = "Greater Noida";
  41. dt.Rows.Add(dtr);
  42. dtr = dt.NewRow();
  43. dtr["UserId"] = 4;
  44. dtr["UserName"] = "Pravesh Khanduri";
  45. dtr["Education"] = "BCA";
  46. dtr["Location"] = "Delhi";
  47. dt.Rows.Add(dtr);
  48. dtr = dt.NewRow();
  49. dtr["UserId"] = 5;
  50. dtr["UserName"] = "Ravi Kumar";
  51. dtr["Education"] = "BCA";
  52. dtr["Location"] = "Delhi";
  53. dt.Rows.Add(dtr);
  54. dtr = dt.NewRow();
  55. dtr["UserId"] = 6;
  56. dtr["UserName"] = "Ainul Hasan";
  57. dtr["Education"] = "MCA";
  58. dtr["Location"] = "Gurgaon";
  59. dt.Rows.Add(dtr);
  60. gvDetails.DataSource = dt;
  61. gvDetails.DataBind();
  62. }
  63. protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
  64. {
  65. if (e.Row.RowType == DataControlRowType.DataRow)
  66. {
  67. CheckBox chkbox = (CheckBox)e.Row.FindControl("chkSelect");
  68. if (e.Row.Cells[3].Text == "BCA")
  69. {
  70. chkbox.Enabled = false;
  71. }
  72. else
  73. {
  74. chkbox.Enabled = true;
  75. }
  76. }
  77. }
  78. }

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.