Tuesday, December 30, 2014

Checkboxs Enable / Disable in Asp.net Gridview Based on Condition in C#, VB.NET

Introduction:

Here I will explain how to enable or disable checkbox in gridview based on condition in asp.net  using C# and VB.NET.

We use gridview rowdatabound event for enable / disable checkbox in gridview based on conditions.
The below Code is the Event for enable / disable checkbox.

protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)e.Row.FindControl("chkSelect");
if (e.Row.Cells[4].Text == "Hyderabad")
{
chk.Enabled = false;
}
else
{
chk.Enabled = true;
}
}
}




The following code is example code in aspx page

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Enable/Disable Checkbox in Gridview based on condtion in Asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvDetails" AutoGenerateColumns="false" CellPadding="5" runat="server" OnRowDataBound="gvDetails_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="UserId" DataField="UserId" />
<asp:BoundField HeaderText="UserName" DataField="UserName" />
<asp:BoundField HeaderText="Education" DataField="Education" />
<asp:BoundField HeaderText="Location" DataField="Location" />
</Columns>
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
 The Code behind file aspx.cs


protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        {
            BindGridview();
        }
}
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)e.Row.FindControl("chkSelect");
if (e.Row.Cells[4].Text == "Hyderabad")
{
chk.Enabled = false;
}
else
{
chk.Enabled = true;
}
}
}


  
protected void BindGridview()
    {
        DataTable dt = new DataTable();       

        dt.Columns.Add("UserId", typeof(Int32));
        dt.Columns.Add("UserName", typeof(string));
        dt.Columns.Add("Education", typeof(string));
        dt.Columns.Add("Location", typeof(string));
        dt.Rows.Add(1, "Muhammed Asad", "BCIT" , "Karachi");
        dt.Rows.Add(2, "Muhammed Ali", "Msc", "Karachi");
        dt.Rows.Add(3, "Madhav Sai", "MS", "Karachi");
        dt.Rows.Add(4, "Asma", "MBA", "Karachi");
        dt.Rows.Add(6, "Faheem Shah", "MBBS", "Karachi");
        dt.Rows.Add(7, "Asif Raza", "B.Tech", "Karachi");
        dt.Rows.Add(8, "Daniel", "CA", "Karachi");
        gvDetails.DataSource = dt;
        gvDetails.DataBind();       
    }
 Damo:

The above example if you see the databound event we created, there is If condition in that I gave "Hyderabad"to Disable the Checkbox. Where condetion will be TRUE it will block/Disable the checkbox for  the user.



No comments:

Post a Comment