I’m trying to get one column of data and stuff it in a combobox but couldn’t get it to work. Can anybody help me out with this please?
Code:
test_DataEntities db = new test_DataEntities();
DataGrid test = new DataGrid();
test.ItemsSource = db.testTbls;
cmbWorkOrder.ItemsSource = test.Columns[2];
You can try:
It returns an
IEnumerable<T>whereTis the type of the property you want to display in the combo box (astringfor instance). I would expect that binding totest.ItemsSourcewill execute the query to fetch the entities from the DB into memory and then the binding tocmbWorkOrder.ItemsSourceonly reads its data from the in-memory collection and doesn’t hit the database again. Sure though I am not.Edit
Perhaps this is a bit cleaner:
You have control now when the query actually gets executed. In the first example it would depend on the binding engine when it will bind the grid and when the combo box to your data which might depend on the order of the controls in XAML (assuming it’s WPF).