Binding to a ComboBox

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
 
R

Rick

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
 
S

Sheng Jiang

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top