Find checkbox column in gridview OnCheckedChange event

130 Views Asked by At

I have few gridview columns with checkboxes control that has an autopostback='true' to update a db.

enter image description here

When i check one of the checkboxes the entire row gets updated which makes me frustrated.

I'm trying to find the checkbox control of the column not the whole gridview

I know that Mycheckbox.checked will always be true because it looks at the "checked values for all" however i want it only to look in the column specified

Is it possible to do something like Checkbox MyCheckbox = findcontrol(*MyColumn*)......

protected void myCheckBox_OnCheckedChange(object sender, EventArgs e)
{    
    CheckBox myCheckBox = (CheckBox)sender;
    GridViewRow row = (GridViewRow)myCheckBox.NamingContainer;
    bool status = myCheckBox.Checked;   
    string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        SqlCommand cmd = new SqlCommand("sp_tblPlanningChecked", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@PlanningID", row.Cells[1].Text);
        cmd.Parameters.AddWithValue("@ThisWeekMinus2", myCheckBox.Checked);
        cmd.Parameters.AddWithValue("@ThisWeekMinus1", myCheckBox.Checked);
        cmd.Parameters.AddWithValue("@ThisWeek", myCheckBox.Checked);
        cmd.Parameters.AddWithValue("@ThisWeekPlus1", myCheckBox.Checked);
        cmd.Parameters.AddWithValue("@ThisWeekPlus2", myCheckBox.Checked);
        cmd.Parameters.AddWithValue("@ThisWeekMinus3", myCheckBox.Checked);
        cmd.Parameters.AddWithValue("@ThisWeekMinus4", myCheckBox.Checked);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }

}
1

There are 1 best solutions below

1
Sujeet Singh On

Could you please try below code

foreach (GridViewRow grow in GridView1.Rows)
{
    CheckBox chkStat = grow.FindControl("chkStatus") as CheckBox;
    int index = grow.RowIndex;
    if (chkStat.Checked)
    {
        //Write some code if checkbox is checked
    }
    else
    {
        //Write some code if checkbox is not checked
    }
}