简体   繁体   中英

How to connect ComboBox to database file in C# for WPF

I am learning how to use C# in visual studio, and now trying to connect a comboBox to a database file with extension .mdb the combobox still can not get any information from the database. I want to the combobox to display anything, description or loadid and i will continue from there.

this is the XAML code

<Window x:Class="test_chose_pic.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:test_chose_pic"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid Background="OldLace">
<StackPanel Orientation="Horizontal" Width="390" Height="24" Background="Beige" >
    <Label Content="select:" Height=" 22" HorizontalAlignment="Right"/>
        <ComboBox  x:Name="ComboBoxZone" Width="120" Height="22" ItemsSource="{Binding}" Margin="0,1,0,0" VerticalAlignment="Top"/>
        <StackPanel>
            <Button Margin=" 26,0" x:Name="btnZone"  Content="Show"  Width="70" Height="20" Click="btnZone_Click" />
        </StackPanel>
</StackPanel>
</Grid>

and the Code:

 public void BindComboBox(ComboBox comboBoxName)
    {
        SqlConnection conn = new SqlConnection(@"Data Source = C:\Users\Mohammed's PC\Desktop\SCEPTER\pacificorpv07_2013.mdb;");
        SqlDataAdapter da = new SqlDataAdapter("Select loadid,description FROM load", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "laods");
        comboBoxName.ItemsSource = ds.Tables[0].DefaultView;
        comboBoxName.DisplayMemberPath = ds.Tables[0].Columns["loads"].ToString();
        comboBoxName.SelectedValuePath = ds.Tables[0].Columns["loadid"].ToString();
    }


    private void btnZone_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Selected ZoneName=" + ComboBoxZone.Text + " and ZoneId=" + ComboBoxZone.SelectedValue.ToString());
    }

for more clarity The database file have table called loads and that table have two column called (loadid) and (description)

The values for DisplayMemberPath and SelectedValuePath properties of a wpf combobox will be column names/property names of the collection that you are used to bind the control. Here in your case you are using a DataTable, so the these values should be the name of the columns in the table. so change the following lines :

 comboBoxName.DisplayMemberPath = ds.Tables[0].Columns["loads"].ToString();
 comboBoxName.SelectedValuePath = ds.Tables[0].Columns["loadid"].ToString();

To this:

 comboBoxName.DisplayMemberPath = "loads";
 comboBoxName.SelectedValuePath = "loadid";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM