Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to make a meal planner, I have a GridView of foods and their nutritional values and a CheckBox row. I want to be able to check as many foods as I wish. Then click the Add Food button and have the information of the checked rows inserted into my database.

Here is what I have which is not working:

 protected void AddFoodbtn_Click(object sender, EventArgs e)
 {

       CheckBox rowcheck = (CheckBox)GridView2.FindControl("SelectFoodchk");
        for (int i = 0; 0 < GridView2.Rows.Count; i++)
        {
            if (rowcheck.Checked)
            {

                GridViewRow viewrow = GridView2.Rows[i];
                string foodid = viewrow.Cells[0].Text.ToString();


            }

        }
    }
share|improve this question
1  
And how exactly is it not working? Do you get an error message? If so, on what line? Or does it simply do nothing? – Renan May 28 at 21:09

1 Answer

foreach (GridViewRow row in grid.Rows)
{
   if (((CheckBox)row.FindControl("chkboxid")).Checked)
   {
       //read the label
   }
}

Looping through GridView rows and Checking Checkbox Control

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.