简体   繁体   中英

Getting data from a column of a dataset to populate items of a combo box

I have created a dataset in my visual studio project which is connected to a Microsoft Acess datebase. I want to populate the values of a particular column in the table into the items of a combo box (ie dropdown box). For this I created a table adapter called "empnames" and a method "GetDataByName()" which selects only the column which is required.

First I tried this
comboBox1.ItemsSource = empnames.GetDataByName().ToString();
For this I get the letters in the table name as different items of the combo box. For example if the table name is emp_data, I get 8 items in combobox ie 'e','m','p','d','a','t','a'. Can you please help me to get proper result.

Thanks

Code:

private void Window_Loaded(object sender, RoutedEventArgs e) { 
SampleDataSetTableAdapters.Emp_dataTableAdapter empnames = new SampleDataSetTableAdapters.Emp_dataTableAdapter();
comboBox1.ItemsSource = empnames.GetDataByName();
} 


This is the code for method to populate the combobox items. The GetDataByName() is designed using table adapter query configuration wizard. Return type is data table.

You should assign the DataTable to the ItemsSource , and set DisplayMemberPath to the name of the column you want to show.

comboBox1.ItemsSource = empnames.GetDataByName();
comboBox1.DisplayMemberPath = "Name"; // assuming there is a "Name" column

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM