You are on page 1of 7

7/10/2014

Databinding With ComboBox in ADO.NET

In Focus

C# Corner Q3, 2014 MVPs Announced

Ask a Question

The Circular Stack, An Advance in D ...

Contribute

C# Corner Search

ARTICLE

READER LEVEL:

Databinding With ComboBox in ADO.NET


Posted by A K in Articles | ADO.NET on January 11, 2012
In this article you will learn to bind data to a ComboBox in various ways.

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

The Circular Stack, An Advance in Data


Structure

02

Abstract Class & Interface: Two Villains of


Every Interview - Part 1

03

Abstract Class & Interface: Two Villains of


Every Interview - Part 2

04

Creating Custom Forum Chat Box: Chapter


1

05

MongoDB CRUD Operations in ASP.Net

06

C# Corner Delhi Developer's Day 13


September, 2014: Official Recap

07

Virtual vs Override vs New Keywords in C#

08

Why Developers Should Focus On


Innovation

09

Tutorial C# Core : Variables

10

ASP.Net MVC4, a Walk-Through


View All

Follow @csharpcorner

Go to smart property of ComboBox control.

6,636 followers

Find us on Facebook

C# Corner
Like

67,851 people like C# Corner.

Follow the given steps.


Step 1 : Check the CheckBox of Use Data Bound Items.

Facebook social plugin

http://www.c-sharpcorner.com/UploadFile/718fc8/databinding-with-combobox-in-ado-net/

1/7

7/10/2014

Databinding With ComboBox in ADO.NET

Step 2: Click on the down arrow of the Data Source.

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

Databinding With ComboBox in ADO.NET

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

Databinding With ComboBox in ADO.NET

Step 8: Click the next button.

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

Databinding With ComboBox in ADO.NET

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.

Run the application.


Output

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

Databinding With ComboBox in ADO.NET

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

WPF ComboBox doesn't bind correctly - Binding


to Nullable Ints
Data Binding from dataset to HTML table in .Net
Databinding in Listbox from XML file
Custom ComboBox
Using New ComboBox ActiveX With VB

DataBinding with a ComboBox in FSharp


Databinding with DataGridView in ADO.NET
Cascading Dropdown & Multiselect option in
ListBox using WPF
Data Binding in Silverlight with RIA and
EntityFramework (Displaying Data)
ComboBox in Silverlight

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/

Oct 25, 2012

6/7

7/10/2014

Databinding With ComboBox in ADO.NET

Type your comment here and press Enter Key....

COMMENT USING
Add a comment...

Also post on Facebook

Posting as Fily Cario Rojas (Not you?)

Comment

Facebook social plugin

MVPs

MOST VIEWED

PHOTOS
CONTACT US

LEGENDS

CODE SNIPPETS
PRIVACY POLICY

NOW

PRIZES

CONSULTING
TERMS & CONDITIONS

AWARDS

TRAINING
SITEMAP

REVIEWS

SURVEY

STUDENTS

CERTIFICATIONS

MEMBERS

Hosted By CBeyond Cloud Services

DOWNLOADS

MEDIA KIT

ABOUT US

LINKS

IDEAS

REPORT ABUSE

2014 C# Corner. All contents are copyright of their authors.

http://www.c-sharpcorner.com/UploadFile/718fc8/databinding-with-combobox-in-ado-net/

7/7

You might also like