Professional Documents
Culture Documents
Databinding With ComboBox in ADO
Databinding With ComboBox in ADO
In Focus
Ask a Question
Contribute
C# Corner Search
ARTICLE
READER LEVEL:
74
1,956
Tweet
Share
Like
29145
TRENDING UP
Introduction
In this article I am describing various techniques of databinding with a ComboBox control. A
ComboBox control shows data in a dropdown list. Before starting the databinding with ComboBox,
let's know its two important properties - DisplayMember and ValueMember.
DisplayMember : This property is used to specify the name of a column ( Database Table's Column
) to be displayed in the ComboBox.
ValueMember : This property is used to specify the name of a column ( Database Table's Column )
to be stored as values of selected item in ComboBox.
I am using a "student" database which has a table "student_detail" with some records. Now take a
Windows Forms application -> Take a label and ComboBox controls as in the following figure.
01
02
03
04
05
06
07
08
09
10
Follow @csharpcorner
6,636 followers
Find us on Facebook
C# Corner
Like
http://www.c-sharpcorner.com/UploadFile/718fc8/databinding-with-combobox-in-ado-net/
1/7
7/10/2014
Step 3: Click at Add Project Data Source link. A new window will open.
Step 4: Select "Database" as Data Source ( By default Database is selected ). Click the next button.
A new window will open.
http://www.c-sharpcorner.com/UploadFile/718fc8/databinding-with-combobox-in-ado-net/
2/7
7/10/2014
Step 5: Select "Dataset" as Database model ( By default Dataset is selected ). Click the next button.
A new window will open.
Step 6: Click the "New Connection" button. A new window will open. In the new window enter the
Server name (I have used dot), write user name and password of your SQL Server and select
database ( I have selected "student" ). Look at the following figure.
Step 7: Click the "ok" button. Now a connection has been made to the student database. You will
look at the "Data Source Configuration Wizard" window.
http://www.c-sharpcorner.com/UploadFile/718fc8/databinding-with-combobox-in-ado-net/
3/7
7/10/2014
Step 9: Click the plus ( + ) sign to explore all tables of the student Database.
Step 10: Again click the plus sign for "student_detail" to show all columns of the Database table.
Check the all CheckBox.
http://www.c-sharpcorner.com/UploadFile/718fc8/databinding-with-combobox-in-ado-net/
4/7
7/10/2014
Step 11: Click the Finish button. Now set the DisplayMember and ValueMember property of
ComboBox. Click at down arrow of Display Member and select "rollno". Same as set "name" as Value
Member property.
TECHNOLOGIES
ANSWERS
BLOGS
VIDEOS
INTERVIEWS
BOOKS
NEWS
CHAPTERS
CAREER ADVICE
JOBS
It will show all values from the "RollNo" column because we have set RollNo to the DisplayMember
property of combobox. Now stop the running program -> go to design window of your application
and write the following code on SelectedIndexChanged event of ComboBox.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = comboBox1.SelectedValue.ToString();
}
Run the application. Select a different value in ComboBox. It will show the student name in Label
regarding to selected Roll Number.
http://www.c-sharpcorner.com/UploadFile/718fc8/databinding-with-combobox-in-ado-net/
5/7
7/10/2014
Now we bind the ComboBox with data by code. Take another ComboBox ( Say CombBox2) and a
Label and write the following code.
using
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.Data.SqlClient;
namespace DataBindingWIthComBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlDataAdapter dadpter=new SqlDataAdapter("select * from
student_detail","server=.;database=student;user=sa;password=wintellect");
DataSet dset = new DataSet();
dadpter.Fill(dset);
comboBox2.DataSource = dset.Tables[0];
comboBox2.DisplayMember = "rollno"; // to display roll no. in combobox
comboBox2.ValueMember = "name"; // to store name as value of combobox for selected
roll no.
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
label2.Text = comboBox2.SelectedValue.ToString();
}
}
}
Run the application. It will do same work as before.
Here are some related resources.
DataBinding with a ComboBox in FSharp
WPF ComboBox doesn't bind correctly - Binding to Nullable Ints
Databind WPF Combobox
Cascading Dropdown & Multiselect option in ListBox using WPF
RELATED ARTICLES
COMMENTS
1 of 1
kamlesh patidar
Thanks dear, Really its very useful for beginners student. I hope any entry level developer , quick get it as read it,
becouse here use franklly word for explnnation.
0 Like
0 Reply
Post Reply
http://www.c-sharpcorner.com/UploadFile/718fc8/databinding-with-combobox-in-ado-net/
6/7
7/10/2014
COMMENT USING
Add a comment...
Comment
MVPs
MOST VIEWED
PHOTOS
CONTACT US
LEGENDS
CODE SNIPPETS
PRIVACY POLICY
NOW
PRIZES
CONSULTING
TERMS & CONDITIONS
AWARDS
TRAINING
SITEMAP
REVIEWS
SURVEY
STUDENTS
CERTIFICATIONS
MEMBERS
DOWNLOADS
MEDIA KIT
ABOUT US
LINKS
IDEAS
REPORT ABUSE
http://www.c-sharpcorner.com/UploadFile/718fc8/databinding-with-combobox-in-ado-net/
7/7