BugsFixer
BugsFixer

Reputation: 377

Bind column from datatable to combobox

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

Upvotes: 2

Views: 882

Answers (3)

GregorMohorko
GregorMohorko

Reputation: 2867

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;

Upvotes: 0

MarkManYUN
MarkManYUN

Reputation: 1

You can try this.

this.myComboBox.datasource=dt;

Upvotes: 0

Alfie
Alfie

Reputation: 2013

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();
}

Upvotes: 1

Related Questions