vendredi 27 août 2021

How do I get the selected Value from dynamically created CheckBox?

I added CheckBox controls dynamically from the database to FlowLayOutPanel. How do I get and store all selected values?

My sample code is here...

private void dynamicCheck()
{
    //throw new NotImplementedException();
    DataSet1TableAdapters.tbl_subjects1TableAdapter ta = 
        new DataSet1TableAdapters.tbl_subjects1TableAdapter();
    DataTable dt = ta.GetData();
    foreach (DataRow row in dt.Rows)
    {
        CheckBox chk = new CheckBox();
        chk.Width = 90;
        chk.Text = row[1].ToString();
        chk.CheckedChanged += new 
                          EventHandler(changeCheck);
        flowLayoutPanel1.Controls.Add(chk);            
    }
}   

private void changeCheck(object sender, EventArgs e)
{
    //throw new NotImplementedException();
    CheckBox chk = sender as CheckBox;
    if (chk.Checked)
    {
        //MessageBox.Show("checked item" + chk.Text);        
    }
}

private void buttonSave_Click(object sender, EventArgs e)
{
}



Aucun commentaire:

Enregistrer un commentaire