In this article I am going to explain you, how to bind ComboBox with the help of TableAdapter.
First create a new wpf project , then right click on the solution and select add new item and select DataSet as in figure 1
After that drag the TableAdapter from the Toolbox into the new added " Dataset1.xsd" as in figure 2.
When you drag the TableAdapter into the Dataset1.xsd file , TableAdapter Configuration wizard will show up.
Select the proper connection string for your database otherwise create new ConnectionString.
When you press next, the new screen will come up, In this screen there is three option , You can choose any option according to your need Here I am selecting the "Create new stored Procedure" option and press next, the new screen will come as in figure 4.
You can write query directly or through Query Bulider. After writing the query press next and give name to the select stored procedure. After pressing Next wizard will ask for the method, Select " Returns a DataTable" Checkbox, then press Next and Finish.
The final window will look likes that.
Now created TableAdapter is ready to use.
Now put one ComboBox in xaml file,
<ComboBox Grid.Row="1" Height="23" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" Margin="143,12,185,0" Name="comboBox1" SelectedValue="{Binding Path=id}" DisplayMemberPath="username" SelectedValuePath="id" VerticalAlignment="Top" />
And write this code in XAML.cs for binding Combobox.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
UserDataSetTableAdapters.UserTableAdapter userTableAdapter = new UserDataSetTableAdapters.UserTableAdapter(); comboBox1.ItemsSource = userTableAdapter.GetData();
}
By this way you can bind the Combobox with the database.