Click here to Skip to main content
15,896,502 members

How to make checkbox true from database value

Umapathi K asked:

Open original thread
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.,
C#
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();
        }
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900