Wrox Programmer Forums
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 17th, 2014, 10:21 AM
Authorized User
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default Checkbox in GridView

Hi all, hope in your help.

If select in GV a check box from 1 row then you want all other rows in the GridView to be disabled leaving the current row enabled...

I tried this code but not working ... can you help me?

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        CheckBox chkTest = (CheckBox)sender;
        GridViewRow GridView1 = (GridViewRow)chkTest.NamingContainer;

        if (chkTest.Checked)
           GridView1.BackColor = System.Drawing.Color.Yellow;

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            DropDownList DDL = (DropDownList)e.Row.FindControl("DDL");

            foreach (GridViewRow gvr in GridView1.Rows)
                if (((CheckBox)gvr.FindControl("chkSelect")).Checked)
                    DDL.Enabled = false;
                    DDL.Enabled = true;
Old April 25th, 2014, 06:58 PM
Friend of Wrox
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts

Have you stepped through it to find out why it not working. I see one thing off the top that I know has frustrated me time and time again is that the find control method does not recursively search for controls and does not find controls that are not a direct a child of the control your calling the method on. Since your working with check box i am assuming its a templated gv so you might check to see if you are actually finding the check box during execution. If your not then write a recursive search for control.

public Control FindControl( Control cntrl, string controlID )
    Control rVal = null; 

     if( cntrl.ID == controlID )
         rVal = cntrl;
          // we do the recursive depth search on all child control until we find it
          foreach( var c in cntrl.Controls )
                rVal = FindControlRecursive( cntrl, controlID );
                if( rVal != null )
                     break; // found quit looking

    return rVal;
I also notice that you try to find a drop down for the current row that was bound, and then iterate through the entire gv looking at each row for a check box, toggling the current bound rows drop down on and off. Is this what you intended or did you intend on enabling the current rows drop down base on what that rows check box was?

You can do this with the recursive search defined above
var ddl = FindControl(e.Row,"DDL");
var chbx = FindControl(e.Row,"chkSelect");
ddl.Enabled  = chkbx.Checked;

Last edited by mmorgan30; February 26th, 2015 at 09:43 PM.. Reason: added search code, update findcontrol code

Similar Threads
Thread Thread Starter Forum Replies Last Post
can't get checkbox value from Gridview akshay144 ASP.NET 3.5 Basics 4 March 31st, 2011 02:52 AM
Gridview and Checkbox krasnokojiy ASP.NET 2.0 Basics 14 February 28th, 2008 12:56 PM
Checkbox in Gridview yukijocelyn ASP.NET 2.0 Basics 0 September 24th, 2007 12:32 AM
GridView Checkbox ItemTemplate angelicagm ASP.NET 2.0 Professional 3 May 31st, 2007 04:44 AM
Checkbox in GridView aliirfan84 ASP.NET 2.0 Professional 1 May 15th, 2007 03:59 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.