Solved

Populate combo box

Posted on 2009-05-05
8
186 Views
Last Modified: 2012-05-06
Please check the code below.  I included 2 functions.  The one that is under the form_load event works.  But I would like to do the same thing using ComboBoxLoad function that is below the first one.  I am having the problem finishing it.  Please help.

private void frmCustomer_Load(object sender, EventArgs e)
        {
            string[] states =
                {"Select state...",
                    "MI","OH","PA","VI"};
            foreach (string state in states)
                cmbStates.Items.Add(state);
            cmbStates.SelectedIndex = 0;
            ComboBoxLoad();
        }
       
        private void ComboBoxLoad()
        {
            string connectionString =
                    @"Data Source=MAIN-HOMEPC\SQLEXPRESS;Initial Catalog=KQC;Integrated Security=True;Pooling=False";
            SqlConnection myConn = new SqlConnection(connectionString);
            myConn.Open();
            string selectStates = "Select * from States";
            SqlDataAdapter stateDataAdapter;
            stateDataAdapter = new SqlDataAdapter(selectStates, myConn);
            DataSet stateDataSet = new DataSet();
            stateDataAdapter.Fill(stateDataSet);
0
Comment
Question by:Aquarus
  • 4
  • 3
8 Comments
 
LVL 9

Expert Comment

by:tculler
Comment Utility
What exactly are you trying to achieve in the ComboBoxLoad method?
0
 
LVL 12

Expert Comment

by:williamcampbell
Comment Utility
 DataSet stateDataSet = new DataSet();
  stateDataAdapter.Fill(stateDataSet);

  cmbStates.DataProvider = stateDatatSet;
0
 

Author Comment

by:Aquarus
Comment Utility
I have a table and the select statement that selects one field from this table.  I am populating the dataset with the result of this select statement and want the data to show in the dropdown list of the combo box. I do not have property DataProvider in my combox properties or methods. THis does not work:
 cmbStates.DataProvider = stateDatatSet;
0
 
LVL 12

Expert Comment

by:williamcampbell
Comment Utility

Sorry thinking of Flex

 try this

cmbStates.DataSource=stateDataSet.Tables[0];
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Aquarus
Comment Utility
I did.  Here again my code with the last line as you suggested.  ATtached is the screenshot with the result.
ScreenShotMyCombo.doc
0
 
LVL 12

Accepted Solution

by:
williamcampbell earned 500 total points
Comment Utility
Fill in the Table Column Name you want to display
add 
 

cmbStates.DisplayMember= ***NAME in TABLE ***;

cmbStates.ValueMember =comboBox1.DisplayMember;

Open in new window

0
 
LVL 12

Expert Comment

by:williamcampbell
Comment Utility
maybe
 
cmbStates.DisplayMember= "State";
cmbStates.ValueMember =cmbStates.DisplayMember;

0
 

Author Closing Comment

by:Aquarus
Comment Utility
Thank you williamcampbell
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

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

8 Experts available now in Live!

Get 1:1 Help Now