Microsoft Access - Bind Dataset with ComboBox

This is a simple article which describes how to bind a dataset to a comboBox.

In this article we assume that user know how to fill a dataset object with desired Data.

If you don't know how to fill a dataset, here is a simple step as follows:


1. Suppose you are using Access database named "ComboDetail".

2. This contains a Table called "Categories"

3. Table contains two fields ProductDetail and ProductPrice.

In your c# code window first add reference to Data namespace like this:

using System.Data.Oledb;

So now you are ready.

 

//create a oledb connection
oleDbConnection cn=new oleDbConnection(sConnectionString);
//create data adapter
oleDbdataAdapter adp=new oleDbdataAdapter("select * from Categories",cn);
//Create a Dataset
DataSet ds=new DataSet();
adp.Fill(ds);
// NOW you hav a data set which contains the table "Categories"
//bind it with comboBox1 as follows

ComboBox1.DataSource=ds.Tables["Categories"];
ComboBox1.DisplayMember="ProductDetail";
// you can also associate other Value to the same combobox like this:
ComboBox1.ValueMember="ProductPrice";
//set the display index for first item to be displayed
ComboBox1.SelectedIndex=0;





// Now you are done with it... I hope this will sort some of your problem. I have taken fullcare for the correctness of the code. if something still is confusing please feel free to discuss.

By Ajay Arora   Popularity  (8095 Views)
Biography - Ajay Arora
Ajay Arora has Done M.Tech( Software engineering). He is working with 'DeltaX Solutions - An offshore software development company'. He has good working exp. in c#.net,asp.net,xml,ado.net,vb.net,vb6, Quality assurance, Metrics,SDLC,Risk Management. http://www.deltaxsolutions.com