DataBinding Combos
Hi All,
Hope someone can help me out.
I have a winform with user details (textboxes, combos etc).
I bind the textboxes to a dataset containing contact data.
The combos are set to a reference dataset as the data source and bound to the contacts dataset.
Like this…
this.cboContactType.DataSource =Global.dsRef.Tables["ContactTypes"];
this.cboContactType.DisplayMember = "ContactTypeName";
this.cboContactType.ValueMember = "ContactTypeName";
this.cboContactType.DataBindings.Add("SelectedValue",Global.dsTrans,"Contacts.ContactType");
The text of the Contacts.ContactTypes column will (should) display in the combo.
I have a button_Click event that allows the user to scroll through the Contact Details one by one.
All the textboxes display correctly and the combos display correctly UNTIL a null occurs after a non-null ContactType.
What is displayed instead is the first row of the Global.dsRef.Tables[“ContactTypes”].
Any ideas?
Thanks
Clarke
I'm unable to reproduce this with a similar scenario and will need some more information. I assume you have a relationship between Contacts.ContactType and ContactTypes.ContactTypeName? if so, it's a bit confusing in that it appears your display value is also your key value - however I'm not sure that has anything to do with your issue.
What should be happening is when you cycle to a ContactType with a value of null (DBNull.Value), the ComboBox.SelectedValue gets set to DBNull.Value. Normally, the ComboBox will cycle through it's items and look for an item with a property "ContactNameType" that has a value of "DBNull.Value". Typically, it won't find this and will set it's value to -1 (blank). In your case, it's going to the first item in the list and I'm not clear why this is. Some things to try:
Change your data binding to the following:
| | this.cboContactType.DataBindings.Add("SelectedValue",Global.dsTrans,"Contacts.ContactType", true); |
Also, add a button that sets the ComboBox.SelectedValue to DBNull.Value and see what happens.
If all else fails, then package up a sample by dumping your two tables to an XML file and file a bug here (I assume this is a VS 2005 issue): http://lab.msdn.microsoft.com/productfeedback/default.aspx.
Joe