Solved

Populate combo box

Posted on 2009-05-05
8
190 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
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!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

713 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