Solved

dynamic combobox contains no items

Posted on 2004-09-16
10
344 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why, and when, to use Windows Workflow 1 32
XAML: Layout 8 25
c# LinkButton OnClientClick 2 34
Word Directory is not in the drop down list 4 25
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

15 Experts available now in Live!

Get 1:1 Help Now