wpf populate combobox from database
  • fill combobox in WPF from database..use below code.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Data.SqlClient;
    using System.Data;

    namespace WpfApplication1
    {
    ///
    /// Interaction logic for Window1.xaml
    ///
    public partial class Window1 : Window
    {
    public Window1()
    {
    InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
    SqlConnection conn = new SqlConnection(@"Data Source=.\SqlExpress;Initial Catalog=YourDatabaseName;Integrated Security=True");

    SqlDataAdapter da = new SqlDataAdapter("Select CountryId,CountryName FROM tblCountry", conn);

    DataSet ds = new DataSet();

    da.Fill(ds, "tblCountry");

    comboBox1.ItemsSource = ds.Tables[0].DefaultView;

    //this column will display as text
    comboBox1.DisplayMemberPath = ds.Tables[0].Columns["CountryName"].ToString();

    //this column will use as back end value who can you use in selectedValue property
    comboBox1.SelectedValuePath = ds.Tables[0].Columns["CountryId"].ToString();
    }
    }
    }

    image
    WPFCombo.JPG
    307 x 303 - 11K

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week

    Optimum Creative