Solved

Populate combo box

Posted on 2009-05-05
8
188 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
ID: 24310037
What exactly are you trying to achieve in the ComboBoxLoad method?
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24310163
 DataSet stateDataSet = new DataSet();
  stateDataAdapter.Fill(stateDataSet);

  cmbStates.DataProvider = stateDatatSet;
0
 

Author Comment

by:Aquarus
ID: 24317258
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 12

Expert Comment

by:williamcampbell
ID: 24318029

Sorry thinking of Flex

 try this

cmbStates.DataSource=stateDataSet.Tables[0];
0
 

Author Comment

by:Aquarus
ID: 24319276
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
ID: 24319854
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
ID: 24319860
maybe
 
cmbStates.DisplayMember= "State";
cmbStates.ValueMember =cmbStates.DisplayMember;

0
 

Author Closing Comment

by:Aquarus
ID: 31578262
Thank you williamcampbell
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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