SIGN UP MEMBER LOGIN:    
ARTICLE

Bind combobox in WPF

Posted by Syed Shakeer Articles | WPF with C# February 17, 2011
How to Bind data dynamically from the Database and Get the ComboBox Selected Text and Value
Reader Level:


This article shows how to bind data dynamically from the database and get the ComboBox selected Text and Value.

Creating a ComboBox Control in .xml:

<Window x:Class="WpfComboBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<
Grid Background="OldLace">
<
StackPanel Orientation="Horizontal" Width="390" Height="24" Background="Beige" >
<Label Content="Select Zone:" Height=" 22" HorizontalAlignment="Right"/>
<
ComboBox  Name="ComboBoxZone" Width="120" Height="22" ItemsSource="{Binding}"/>
<StackPanel>
<Button Margin=" 26,0" Name="btnZone"  Content="Show"  Width="70" Height="20" Click="btnZone_Click" />
</StackPanel>
</
StackPanel>
</
Grid>
</
Window>
In ComboBox Element set the attribute ItemSource="{Binding}".

Get Data from a DataBase table and Binding to ComboBox as follows:

using System.Data;
using System.Data.SqlClient;
namespace WpfComboBox
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
BindComboBox(ComboBoxZone);
}
public void BindComboBox(ComboBox comboBoxName)
{
SqlConnection conn = new SqlConnection("your connection string");
SqlDataAdapter
da = new SqlDataAdapter("Select ZoneId,ZoneName FROM tblZones", conn);
DataSet ds = new DataSet();
da.Fill(ds,
"tblZones");
comboBoxName.ItemsSource = ds.Tables[0].DefaultView;
comboBoxName.
DisplayMemberPath = ds.Tables[0].Columns["ZoneName"].ToString();
comboBoxName.
SelectedValuePath = ds.Tables[0].Columns["ZoneId"].ToString();
}
}


Here DisplayMemberPath helps to display Text in the ComboBox.

SelectedValuePath helps to store values like a hidden field.

When you Run the Program the Output with ComboBox Items is as follows:

BindComWPF1.gif

Get the Text and Value of Selected ComboBox Item:

private void btnZone_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Selected ZoneName="+ComboBoxZone.Text+" and ZoneId="+ ComboBoxZone.SelectedValue.ToString());
}
The Text Property helps get the selected Item.

SelectedValue Property helps you to get the hidden value of Combobox selected Item.

BindComWPF2.gif

Thanks for Reading my article!

Shakeer

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 

..can u please tel how can i use the same for a listbox instead of a combobox...i want to display the contents of only one field from my table ...can u help ?..it shows system.data.datarowview instead of actual data

Posted by shriram iyer Mar 04, 2011

nice work bob

Posted by shriram iyer Mar 04, 2011

Thanks Knights,Dennis and Sapna..

Posted by Syed Shakeer Feb 18, 2011

Good tutorial. Keep up the nice work!

Posted by Sapna Feb 17, 2011

Thanks for Sharing... Dennis

Posted by Dennis Triplett Feb 17, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor