Solved

dynamic combobox contains no items

Posted on 2004-09-16
10
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

705 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