-2

Possible Duplicate:
Looping through DataGrid rows and Checking Checkbox Control

I currently have a GridView which displays data from a Student Table, there is two template fields in the gridview: Checkbox and Label (the label template field displays the StudentID). I have a button on the page which when the user clicks the button I need to loop through each row in the GridView, then find the CheckBox then I need to check if the checkbox is checked or not. If the checkbox is checked I need to add the value in the Label Template Field to a different table in the database. How do I go about acheiving this? I am using C# Code.

Community
  • 1
  • 1
user715115
  • 159
  • 1
  • 3
  • 13
  • 3
    why are you asking again ?http://stackoverflow.com/questions/5743099/looping-through-datagrid-rows-and-checking-checkbox-control , Ask your doubts in the same question – V4Vendetta Apr 21 '11 at 11:37

1 Answers1

0

Actually you can get this information using the OnItemCommand. Bind the ID to the CommandArgument

protected void GVSearchResults_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            foreach (GridViewRow gvr in gvSearchResults.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    if (((Label)gvr.FindControl("lblStudentID")).Text == e.CommandArgument.ToString())
                    {
                        bool isChecked = ((CheckBox)gvr.FindControl("cbStudent")).Checked;
                        int count = 0;
                        if (e.CommandName == "Save")
                        {
                            this.SaveStudentcheck(int.Parse(e.CommandArgument.ToString()));
                        }
                        break;
                    }
                }
            }
            gvSearchResults.DataBind();            
        }
Chad
  • 1,512
  • 1
  • 16
  • 40