jeudi 8 avril 2021

How to Retrieve Multiple Check Boxes added to single Column in C#?

I have 7 checkboxes the checkbox text is Days name e.g Mon, Tue, Wed..... I Want to retrieve data to my Checkboxes which are added to a single column

Add button code

private void button17_Click(object sender, EventArgs e)
        {
            String s = "";
            foreach (Control c in this.Controls)
            {

                if (c is CheckBox)
                {

                    CheckBox b = (CheckBox)c;
                    if (b.Checked)
                    {

                        s = b.Text + "," + s;

                    }
                    s = s.TrimEnd(',');
                }
            }

            cmd = new SqlCommand("update AddWork set NWorkDay=@name,Days=@Wday,TimePday=@Hours where ID=@id", con);
            con.Open();
            cmd.Parameters.AddWithValue("@id", ID);
            cmd.Parameters.AddWithValue("@name", noWork.Text);
            cmd.Parameters.AddWithValue("@Wday", s);
            cmd.Parameters.AddWithValue("@Hours", Hours.Text);


            cmd.ExecuteNonQuery();
            MessageBox.Show("Record Updated Successfully");

            con.Close();
            ShowData();
            ClearData();
        }

Adding to my database is not the problem it is working when I click a column in a view I can get the hours and no of work values but I cant get the check boxes values ticked i need solution to get them in check boxes

data grid view code

 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

           //dataTable is my data grid view name

            ID = Convert.ToInt32(dataTable.Rows[e.RowIndex].Cells[0].Value.ToString());
            noWork.Text = dataTable.Rows[e.RowIndex].Cells[1].Value.ToString();
            
            Hours.Text = dataTable.Rows[e.RowIndex].Cells[3].Value.ToString();
        }



Aucun commentaire:

Enregistrer un commentaire