0

I have a GridView and I put checkboxes inside all its cells. I want to do the following: if the checkbox checked by the user, this means Yes which will be stored in the database else, if the checkbox unchecked by the user, this means No and no need to post anything to the database.

I know I need now to identify each checked checkbox and know which cell this checked checkbox is underneath it.

Any idea about how to do that? Could anyone give me the basic piece of code for doing this?

user976711
  • 115
  • 5
  • 16

2 Answers2

0

I use something similar to this:

    foreach (GridViewRow row in gvYourGridView.Rows)
    {
        CheckBox ck = ((CheckBox)row.FindControl("YourCheckBoxName"));

        if (ck.Checked)
        {
            //If checked is true, update database.
        }
    }
Valeklosse
  • 1,017
  • 3
  • 19
  • 36
0
            int counter = 0;
            foreach (GridViewRow rowitem in gvYourGridView.Rows)
            {
                if (((CheckBox)rowitem.Cells[0].FindControl("chk")).Checked == true)//i consider that the check box is in the first column index ---> 0
                {
                    counter++;
                }
            }
            /////////////////////////////////////////////////////////////
            if(counter == 0) //no checks
            {

             //show some message box to clarify that no row has been selected.

            }
            /////////////////////////////////////////////////////////////
            if (counter == 1) //one check
            {
                //Do something
            }
            /////////////////////////////////////////////////////////////
            if (counter > 1) //more than one check
            {
               //Do something    
            }

            gvYourGridView.DataBind();
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392