.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Bind combobox in wpf

Posted By:Syed Shakeer Hussain       Posted Date: January 14, 2011    Points: 75    Category: WPF    URL: http://www.dotnetspark.com  

The below article shows you 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 1)DisplayMemberPath helps you for displaying Text in ComboBox.

     2)SelectedValuePath helps you for storing values like hidden field.

When you Run the Program the Output with ComboBox Items as Follows:

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());

}

Here 1)Text Property helps you to get the displaying selected Item.

      2)SelectedValue Property helps you to get the hidden value of combobox selected Item.

Thanks for Reading my article!

Shakeer


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend