2

I am trying to bind DataTable column to combobox, I have DataTable Names "Types" with one column, the header of this column is "Type".

I have tried the following:

private DataTable dt;
public MainWindow()
{
   InitializeComponent();
   dt=Query();// return datatable from the database
   myComboBox.DataContext =dt;
}

but the combobox is still empty (the datatable isn't)

I have tried this answer, but in my C# wpf I have no myComboBox.ComboBox.DataSource property

BugsFixer
  • 377
  • 2
  • 15

3 Answers3

1

Try this:

private DataTable dt;
public MainWindow()
{
   InitializeComponent();
   dt=Query();// return datatable from the database
   myComboBox.ItemsSource = dt.AsEnumerable().Select(x => x["Type"].ToString()).ToList();
}
Alfie
  • 1,903
  • 4
  • 21
  • 32
0

Use ComboBox.ItemsSource.

You can set it directly if your DataTable inherits from IEnumerable, otherwise create a List from your DataTable.

Example code (assuming that DataTable inherits from IEnumerable):

myComboBox.ItemsSource = dt;
GregorMohorko
  • 2,739
  • 2
  • 22
  • 33
0

You can try this.

this.myComboBox.datasource=dt;