Binding to a ComboBox

  • Thread starter Thread starter Bill Gower
  • Start date Start date
B

Bill Gower

I have a combo box in which I want to display the distinct values in a field
in a table.

Depending on the table name and the field name entered by the user, I build
an sql statement

_Sql = "Select distinct " + _fieldName + " from " + _tableName

and fill a dataset with the results

How do I bind this to a combobox?

cbFieldValues.DataSource = ds.Table[0].DataSet;
cbFieldValues.DisplayMember = ?;
cbFieldValues.DisplayValue = ?;

Thanks
Bill
 
Your senario of binding to DisplayValue and DisplayMemember does not make
sense when you only want to display a single list. DisplayValue and Member
are used with you want a "Lookup" combobox like if you have a customer
number and want to display the customer name in the combobox.

I think for your requirements you would simply bind the "fieldName" to the
text in the combobox. Leave the DisplayValue and DisplayMembers blank.

Rick
 
you can have a bindingsource as your combobox's datasource.

this.comboBoxEmployee.DataSource = this.employeeBindingSource;
this.comboBoxEmployee.DisplayMember = "EmployeeName";//the name of of the
item text property of the BusinessLayer.Employee class
this.comboBoxEmployee.ValueMember = "EmployeeId";//the name of of the item
value property of the BusinessLayer.Employee class
this.employeeBindingSource.DataSource = typeof(BusinessLayer.Employee);

private void ReloadEmployeeComboBox()
{
List<Employee> employees = new List<Employee>();
AutoCompleteStringCollection
employeeAutoCompleteStringCollection = new AutoCompleteStringCollection();
foreach (Employee employee in Employee.GetEmployees())
{
employees.Add(employee);
employeeAutoCompleteStringCollection
..Add(employee.EmployeeName);
}
comboBoxEmployee.AutoCompleteCustomSource =
employeeAutoCompleteStringCollection;
employeeBindingSource.DataSource = employees;
}
 
Back
Top