Solved

dynamic combobox contains no items

Posted on 2004-09-16
10
341 Views
Last Modified: 2008-03-10
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
Comment
Question by:dotarj
  • 5
  • 3
  • 2
10 Comments
 
LVL 10

Expert Comment

by:123654789987
ID: 12072870
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
 

Author Comment

by:dotarj
ID: 12072948
Hello 123654789987,

Thanks for your fast reply. Doesn't this:

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

do the same thing?

Bye,

.arj
0
 
LVL 10

Expert Comment

by:123654789987
ID: 12073100
U can try this - as it is just a line change :-)
0
 

Author Comment

by:dotarj
ID: 12073150
I tried already but it doesn't work...

Bye,

.arj
0
 
LVL 3

Expert Comment

by:GrumbleBot
ID: 12074988
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:dotarj
ID: 12076572
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
 
LVL 3

Accepted Solution

by:
GrumbleBot earned 100 total points
ID: 12078692
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
 

Author Comment

by:dotarj
ID: 12078937
Hi GrumbleBot,

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

Bye,

.arj
0
 
LVL 3

Expert Comment

by:GrumbleBot
ID: 12079090
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
 

Author Comment

by:dotarj
ID: 12086634
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now