1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
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();
}
} |
Partager