1

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();
    }

}
user9405863
  • 1,506
  • 1
  • 11
  • 16
Nils
  • 516
  • 3
  • 9
  • 33
  • I have read your question a few times but I still don't get what you are trying to do. So you have `column1` and want to get all the `CheckBox`es that are `Checked == true` ? – L. Guthardt Apr 16 '18 at 10:13
  • This is just utterly confusing. In your picture are 7 columns and each column has like 10 `Checkboxes`. – L. Guthardt Apr 16 '18 at 11:17
  • What the heck does that mean: `iim trying to find out how i can use "findcontrol" of a column insted of using myCheckBox as the only checkbox sender` ? So you want to find a control? Please just answer with `Yes` or `No`, in order to keep it simple. Furthermore you dont want to use each comboBox`s sender in its event as a converted `ComboBox` event, what else do you want to use then? What is your goal in total? – L. Guthardt Apr 16 '18 at 11:38
  • Ok so let's imagine we are in `Column` column1. Now you want to get all the `CheckBoxes` in column1 that are checked right? `Yes` or `No`? – L. Guthardt Apr 16 '18 at 12:05
  • But now you talk about a (implifies *one* this in this example) column. But this one column has quite a few CheckBoxes. So how do you imagine just getting true/false when there can be different CheckBoxes with either checked/unckecked? – L. Guthardt Apr 16 '18 at 12:11

1 Answers1

1

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
    }
}
dtlvd
  • 457
  • 1
  • 8
  • 21
Sujeet Singh
  • 165
  • 3
  • 13
  • I tweeked your code a bit but this line: CheckBox chkStat = grow.FindControl("chkStatus") as CheckBox; was the main line i was after. – Nils Apr 16 '18 at 21:50