Solved

dynamic combobox contains no items

Posted on 2004-09-16
10
349 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
Independent Software Vendors: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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