• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

dynamic combobox contains no items

I made a form which dynamically builds itself. The control specs, that are needed to create the controls on the form, are in my database. My problem is that when I've created a dynamic combobox, and i want to change the selectedindex, the quickwatch says that the combobox doesn't contain any items (when I see the combobox in my form it does contain 6 items). After looking around in the quickwatch for some time, it suddenly says that there are 6 items (I only looked in quickwatch, I didn't step through the code further). Here is how I create the combos:

ComboBox combobox = new ComboBox();
combobox.Size = new System.Drawing.Size(200, 20);

combobox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
combobox.Name = vds.Tables[0].Rows[i]["vrg_Vraag_id"].ToString();
combobox.Location= new Point(144, locY);
                  
string Query = "SELECT tbi_Tabelitem_id, tbi_Omschrijving FROM Tabelitems WHERE tbi_Tabel=1";                        
                                    
DataSet tds = functions.ExecuteDataSet(Query);

combobox.DataSource = tds.Tables[0];
combobox.DisplayMember = tds.Tables[0].Columns[1].ColumnName;
combobox.ValueMember = tds.Tables[0].Columns[0].ColumnName;

((Form)m_Parent).Controls.Add(combobox);

Does anyone know how this can happen?

.arj
0
dotarj
Asked:
dotarj
  • 5
  • 3
  • 2
1 Solution
 
123654789987Commented:
See the following sample

   public ListBoxSample3()
        {
       
            this.AutoScaleBaseSize = new Size(5, 13) ;
            this.ClientSize = new Size(292, 181) ;
            this.Text = "ListBox Sample3" ;

            ListBox1.Location = new Point(24, 16) ;
            ListBox1.Name = "ListBox1" ;
            ListBox1.Size = new Size(232, 130) ;
           


            textBox1.Location = new Point(24, 160) ;
            textBox1.Name = "textBox1" ;
            textBox1.Size = new Size(240, 24) ;

            this.Controls.AddRange(new Control[] {ListBox1, textBox1}) ;  //U are missing this line

            // Populates the list box using DataSource.
            // DisplayMember is used to display just the long name of each state.
            ArrayList USStates = new ArrayList()    ;
            USStates.Add(new USState("Alabama", "AL"));
            USStates.Add(new USState("Washington", "WA"))  ;
            USStates.Add(new USState("West Virginia", "WV"));
            USStates.Add(new USState("Wisconsin", "WI")) ;
            USStates.Add(new USState("Wyoming", "WY"));

            ListBox1.SelectedValueChanged += new EventHandler(ListBox1_SelectedValueChanged);
            ListBox1.DataSource = USStates ;
            ListBox1.DisplayMember = "LongName"      ;
            ListBox1.ValueMember = "ShortName" ;

        }
0
 
dotarjAuthor Commented:
Hello 123654789987,

Thanks for your fast reply. Doesn't this:

((Form)m_Parent).Controls.Add(combobox);

do the same thing?

Bye,

.arj
0
 
123654789987Commented:
U can try this - as it is just a line change :-)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
dotarjAuthor Commented:
I tried already but it doesn't work...

Bye,

.arj
0
 
GrumbleBotCommented:
I tried to emulate your code the best I could. I just tied it to a table in northwind database. I found that without the Fill command that the combobox is empty as expected. After adding that line the combobox fills with the data. Not sure if maybe you are missing that or if the call to ExecuteDataSet(Query) accomplishes That. As far as I can see your combobox creation and setup are correctly coded. I think there is something wrong with the dataset code. Here is my emulated code. Let me know if I'm missing something here.

                  sqlDataAdapter1.Fill(dataSet11);

                  ComboBox combobox = new ComboBox();
                  combobox.Size = new System.Drawing.Size(200, 20);

                  combobox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                  combobox.Name = "Test";
                  combobox.Location= new Point(144, 20);
               
                  //string Query = "SELECT tbi_Tabelitem_id, tbi_Omschrijving FROM Tabelitems WHERE tbi_Tabel=1";                    
                             
                  //DataSet tds = functions.ExecuteDataSet(Query);

                  combobox.DataSource = dataSet11.Tables[0];
                  combobox.DisplayMember = dataSet11.Tables[0].Columns[1].ColumnName;
                  combobox.ValueMember = dataSet11.Tables[0].Columns[0].ColumnName;

                  this.Controls.Add(combobox);


Hope this helps.
Good Luck
0
 
dotarjAuthor Commented:
Hello GrumbleBot,

Thank you for your reply. The function ExecuteDataSet handles everything from database to dataset. ExecuteDataSet opens a connection fills a dataadapter with data from the database, puts it in the dataset and returns the dataset. That is why I only have to use this function in my code to get data from the database. The functions works good, I use it in my program a lot. I don't have to worry about connections anymore.

Bye,

.arj
0
 
GrumbleBotCommented:
Hi arj,
I may have misread your question earlier. You said it is failing on the SelectIndex. Where exactly are you setting the SelectedIndex. I did an experiment and placed the selectindex right after the Controls.Add() and it failed. I then add an event for the form Load Event and placed the SelectIndex at that point and all is fine.

            private void Form1_Load(object sender, System.EventArgs e)
            {
                  combobox.SelectedIndex = 3;
            }


Hope this helps.

Good Luck
0
 
dotarjAuthor Commented:
Hi GrumbleBot,

It works indeed. Do you have any idea why it doesn't work while initializing of the form?

Bye,

.arj
0
 
GrumbleBotCommented:
As far as I know the data doesn't get initialized within the constructor. once the contructor finishes then the data is initialized.

Good Luck
0
 
dotarjAuthor Commented:
Hi GrumbleBot,

Thank you for your solution! I also found that you cannot use dialogresult while initializing the form. I now use the load event to build my forms, and it works great!

Thanks!

.arj
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now