Click here to Skip to main content
11,720,785 members (78,588 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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()
        {            
            try
            {
                gn.cnopen();
                string fetch_group_name = "select group_id,group_name,max_items from menu_group";
                SqlCommand cmd = new SqlCommand(fetch_group_name, gn.cn());
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
		 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, gn.cn());
 
                            get_menus1.Fill(fill_menus1);
                            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.Items.Add(li1);
                                ch1.Location = new System.Drawing.Point(x_axis, y_axis);
                                x_axis += 10;
                                y_axis += 10;
                            }
                            Dhal.Controls.Add(ch1);
                        }
		}
	}
 

 

Below code to retreive values from database.
 
public void load_breakfastitems_toedit()
        {
            try
            {
                gn.cnopen();
                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, gn.cn());
                DataTable getdata = new DataTable();
 
                SqlDataAdapter filldata = new SqlDataAdapter(cmd);
                filldata.Fill(getdata);
 
                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);
                    dt1.Columns.Add(cd);
                    dt1.Columns.Add(menuitem);
                    DataRow[] dr;
 
                    dr = getdata.Select("menu_group_id=" + 1 + "");
                    foreach (DataRow temp in dr)
                    {
                        dt1.ImportRow(temp);
                    }
                  
                    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)
            {
                MessageBox.Show(err.Message);
            }
        }
 
        private void button4_Click(object sender, EventArgs e)
        {
            load_breakfastitems_toedit();
        }
Posted 30-Nov-12 2:29am
Edited 30-Nov-12 20:48pm
v4
Comments
Sergey Alexandrovich Kryukov at 30-Nov-12 17:53pm
   
And what's the problem?
--SA
Umapathi K at 30-Nov-12 23:38pm
   
checkboxes not getting check if items matches
Akbar Ali Hussain at 30-Nov-12 19:28pm
   
Is the checkboxlist filled with data?
Umapathi K at 30-Nov-12 23:37pm
   
yes its filled with data.., when form get load.
Er. Vikas Sangal at 1-Dec-12 2:08am
   
ch11 is your checkboxlist ?
Umapathi K at 1-Dec-12 2:49am
   
yes its checkboxlist name [please see my updated codings above]
Er. Vikas Sangal at 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 at 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: bad
good
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
  Permalink  
Comments
Umapathi K at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 2,179
1 OriginalGriff 924
2 F-ES Sitecore 640
3 Maciej Los 609
4 Richard MacCutchan 540


Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 1 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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