Click here to Skip to main content
14,265,815 members
Rate this:
Please Sign up or sign in to vote.
See more:
It is a windows application, I am retrieving data (items) from database into form.
In the form many checkboxlist will appear dynamically to list out the items. I used button click event to fetch data from database after fetching data (items).
If items exists in the checkboxlist (inside many checkboxes are created) then correct check box should get ticked.
I used below coding to finish this but unable to achieve the task [note: I tried in datagridview its appearing fine, but problem in checkboxlist]
Here ch11 is a checkboxlist name.

From the database I am retrieving valued like
menugroupid menuitems
1            33,
1            55,

please help me out.,
I have created checkboxlist dynamically using below coding
NOte:Here Dhal is a groupbox name.

  CheckedListBox ch1 = new CheckedListBox();

 public void menu_items_in_groupbox()
                string fetch_group_name = "select group_id,group_name,max_items from menu_group";
                SqlCommand cmd = new SqlCommand(fetch_group_name,;
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
		 if (dt.Rows[j]["group_name"].ToString() == "dhal")
                             max2 = Convert.ToInt32(dt.Rows[j]["max_items"]);
                            dhalitems.Text = "Any"+max2.ToString();
                            group2 = Convert.ToInt32(dt.Rows[j]["group_id"]); 

                            string menu_items1 = "select itemid,item_name from menu_items where group_id=";
                            menu_items1 = menu_items1 + "(select group_id from menu_group where group_name='dhal')";
                            DataTable fill_menus1 = new DataTable();
                            SqlDataAdapter get_menus1 = new SqlDataAdapter(menu_items1,;

                            int x_axis = 12, y_axis = 34;

                            for (int k = 0; k <= fill_menus1.Rows.Count - 1; k++)

                                ListItem li1 = new ListItem();
                                li1.Text = fill_menus1.Rows[k]["item_name"].ToString();
                                li1.Value = fill_menus1.Rows[k]["itemid"].ToString();
                                ch1.Location = new System.Drawing.Point(x_axis, y_axis);
                                x_axis += 10;
                                y_axis += 10;

Below code to retreive values from database.

public void load_breakfastitems_toedit()
                string fetch_br_items = "select menu_items_id,menu_group_id from MENU_ITEMS_CHOOSED_HALLBOOKED_CUSTOMERS";
                fetch_br_items = fetch_br_items + " where menu_id=1 and hall_booked_id=" + mainhall_id + "";
                cmd = new SqlCommand(fetch_br_items,;
                DataTable getdata = new DataTable();

                SqlDataAdapter filldata = new SqlDataAdapter(cmd);

                if (getdata.Rows.Count > 0)
                    dataGridView1.DataSource = getdata;                 

                    DataTable dt1 = new DataTable();
                    DataColumn cd = new DataColumn("menu_group_id");
                    DataColumn menuitem = new DataColumn("menu_items_id");
                    menuitem.DataType = typeof(string);

                    cd.DataType = typeof(int);
                    DataRow[] dr;

                    dr = getdata.Select("menu_group_id=" + 1 + "");
                    foreach (DataRow temp in dr)
                    string var = dt1.Rows[0]["menu_items_id"].ToString();
                    string[] array = var.Split(',');

                    foreach (ListItem item in this.ch11.Items)
                        foreach (string s in array)
                            if (item.Value == s)
                                item.Selected = true;
            catch (SqlException err)

        private void button4_Click(object sender, EventArgs e)
Updated 30-Nov-12 20:48pm
Sergey Alexandrovich Kryukov 30-Nov-12 17:53pm
And what's the problem?
Umapathi K 30-Nov-12 23:38pm
checkboxes not getting check if items matches
Akbar Ali Hussain 30-Nov-12 19:28pm
Is the checkboxlist filled with data?
Umapathi K 30-Nov-12 23:37pm
yes its filled with data.., when form get load.
Er. Vikas Sangal 1-Dec-12 2:08am
ch11 is your checkboxlist ?
Umapathi K 1-Dec-12 2:49am
yes its checkboxlist name [please see my updated codings above]
Er. Vikas Sangal 1-Dec-12 2:56am
use my solution, don't use array use directly datatable to check ur data to checkbox data and checked if they are same.
Umapathi K 1-Dec-12 3:04am
in datatable i am getting items value like this 31,32,33 thats why i used to array to split values

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

for (int j = 0; j < checkedListBox1.Items.Count; j++)
       foreach (DataRow d in dt.Rows)
            if (checkedListBox1.Items[j].ToString() == d[0].ToString())
                   checkedListBox1.SetItemChecked(j, true);

may be it helps you
Umapathi K 1-Dec-12 3:03am
here i am getting name of items. but i want values because in checkboxlist checkbox text is "itemnames" but value is itemcode., using your given updation loop not entering inside

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100