Windows Develop Bookmark and Share   
 index > Windows Forms General > Combobox binding
 

Combobox binding

Can anyone tell me why can't I execute this command:

cmbTipContact.DataBindings.Add("ValueMember",dt.DefaultView,"TipContact");

the "TipContact" column in the datatable is of Int datatype (I tried string aswell).

Using Binding Context, how can I accomplish this binding?

On another note, this works:

cmbTipContact.DataSource=dt;

cmbTipContact.ValueMember="TipContact";

cmbTipContact.DisplayMember="Name";

but it takes too long to execute, so It slows down the loading of the form considerably.

Thank You in advance.

ArleKin  Monday, March 17, 2008 3:42 PM

Hi,

the code VS is generates is:

//

// contactNameComboBox

//

this.contactNameComboBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.customersBindingSource, "ContactName", true));

The main difference between your first and your second approach is that the first one fills the combobox with one item and your second way will fill the combobox with all rows in your table TipContact. This is normally used for lookup tables if you want to give your customer a more meaningful information than just the primary key. In this case you define the ValueMember to your Primary Key (in Lookup Table) = Foreign Key (in the Table you display) and set the DisplayMember to string field which gives the user more information (like CustomerName).

Since all the rows are loaded into the combobox it takes longer that your first way.

So if you bind to property "Text" instead of "ValueMember" the first way should work. If you want to use the second way (for a lookup operation) you have to bind to "SelectedValue" instead of "ValueMember".

regards

Philipp

Philipp Merz  Monday, March 17, 2008 5:14 PM

Hi,

the code VS is generates is:

//

// contactNameComboBox

//

this.contactNameComboBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.customersBindingSource, "ContactName", true));

The main difference between your first and your second approach is that the first one fills the combobox with one item and your second way will fill the combobox with all rows in your table TipContact. This is normally used for lookup tables if you want to give your customer a more meaningful information than just the primary key. In this case you define the ValueMember to your Primary Key (in Lookup Table) = Foreign Key (in the Table you display) and set the DisplayMember to string field which gives the user more information (like CustomerName).

Since all the rows are loaded into the combobox it takes longer that your first way.

So if you bind to property "Text" instead of "ValueMember" the first way should work. If you want to use the second way (for a lookup operation) you have to bind to "SelectedValue" instead of "ValueMember".

regards

Philipp

Philipp Merz  Monday, March 17, 2008 5:14 PM

Thank You very much for Your thorough answer. It was extremly helpfull.

ArleKin  Tuesday, March 18, 2008 7:01 AM

You can use google to search for other answers

Custom Search

More Threads

• Just-in-time debugging error
• Where can I find some free icons
• SettingsDescriptionAttribute - How is it used?
• How to drag and drop in the same listbox to reorder items?
• My application does not show in the Windows Task Manager
• How to bypass the global graphics scaling with respect to PENWIDTH
• keypress event in Datagridview
• Where can I find the REAL cool buttons, tab's and other graphics
• working on picture layers?
• Encoding problem