Find checkbox column in gridview OnCheckedChange event RRS feed

  • Question

  • User2091173246 posted

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

    <asp:CheckBox ID="CheckBox1" AutoPostBack="true" Checked='<%#Convert.ToBoolean(Eval("[This Week - 2]"))%>' OnCheckedChanged="myCheckBox_OnCheckedChange" runat="server" Style="text-align: center" />

    <asp:CheckBox ID="CheckBox1" AutoPostBack="true" Checked='<%#Convert.ToBoolean(Eval("[This Week - 1]"))%>' OnCheckedChanged="myCheckBox_OnCheckedChange" runat="server" Style="text-align: center" />

    and a few more....

    How can i just focus on column "This week -2" and look if that has true of false value checked?

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


    This code "works" however it updated both columns to true because myCheckbox.checked is "always true"  if one of the column rows are checked.

    Can tweek my code fo only focus on "This week -2" column?

    Monday, April 16, 2018 12:47 PM

All replies

  • User283571144 posted

    Hi Skwizzy,

    According to your codes, I have created a test demo on my side, it works well.

    I could get the right check box chekced value in the oncheckedchanged method.

    More details, you could refer to below codes and result image:


        <form id="form1" runat="server">
                <asp:GridView ID="GridView1" runat="server">
                        <asp:TemplateField  HeaderText="This Week - 2" ShowHeader="true">
                            <ItemTemplate >
                                <asp:CheckBox ID="CheckBox2" AutoPostBack="true" Checked='<%#Convert.ToBoolean(Eval("[This Week - 2]"))%>' OnCheckedChanged="CheckBox1_CheckedChanged" runat="server" Style="text-align: center" />
                        <asp:TemplateField  HeaderText="This Week - 1" ShowHeader="true">
                                <asp:CheckBox ID="CheckBox3" AutoPostBack="true" Checked='<%#Convert.ToBoolean(Eval("[This Week - 1]"))%>' OnCheckedChanged="CheckBox1_CheckedChanged" runat="server" Style="text-align: center" />


            protected void Page_Load(object sender, EventArgs e)
                if (!IsPostBack)
            public void LoadData()
                DataTable d1 = new DataTable() { Columns = { "ID", "Product_Name", "Type" , "This Week - 2", "This Week - 1" } };
                d1.Rows.Add("1", "aa", "bb", true, true);
                d1.Rows.Add("2", "bb", "cc", true,true);
                d1.Rows.Add("3", "cc", "dd", true, true);
                GridView1.DataSource = d1;
            protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
                CheckBox myCheckBox = (CheckBox)sender;
                bool status = myCheckBox.Checked;


    Best Regards,


    Tuesday, April 17, 2018 6:40 AM
  • User2091173246 posted

    Thank you i will look at this.

    Tuesday, April 17, 2018 7:36 AM