c# combobox fill from database

2016/05/04 20:26


To get only the unique /distinct/ data table records from any database connected to DataSet in C# project and to put them inside combobox.

Fill the table adapter with the records from database.

this.myDBTable_TableAdapter.Fill(this.myDB_DataSet.myDBTable);

Create a view containing the database table.

DataView view = new DataView(this.myDB_DataSet.myDBTable);

Copy only the distinct records to new data table.

DataTable distinctValues = view.ToTable(true, "column_name");

For the combobox created, set the data source and display member properties.

private System.Windows.Forms.ComboBox my_ComboBox;

my_ComboBox.DataSource = distinctValues ;

my_ComboBox.DisplayMember = "column_name";