Reputation: 377
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
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
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