Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > bound combobox.text changes when list of items is updated
 

bound combobox.text changes when list of items is updated

Hello all,

Hopefully there is someone out there who has run across this issue and perhaps will share a solution.

I have a form with two comboboxes. Both boxes are bound to one table that uses values from another table to perform a lookup. The first combobox controls the second combobox's list items. For example, the first combobox list portion has all the countries of the world and the second combobox list portion holds regions within each country based on the value stored in the country combobox. When the country combobox text portion changes via record navigation and updates the second combobox's list items, the text portion of the second combobox is changed to the first entry in the list instead of the value stored in the database.

Does anyone know of way to avoid this so the text box is not changed when the list is updated?

Thank you in advance for any assistance you may give.
cbGifford  Monday, September 21, 2009 3:51 AM
Ok, I solved the problem though I do not know what actually caused the issue in the first place. I deleted the comboboxes, then the binding sources and tableadapters from the form. Then I deleted the dataset itself. I recreated the dataset then the comboboxes and set the bindings and datasource as apropriate and the problem went away.

I can only assume that there was some type of corruption in the dataset designer files. Thanks again for the assistance.
  • Marked As Answer bycbGifford Tuesday, September 22, 2009 2:19 AM
  •  
cbGifford  Tuesday, September 22, 2009 2:19 AM
Could you post the relevant code? Thanks.
~~Bonnie Berent [C# MVP]

(new blog --- not many posts yet --- be patient)
geek-goddess-bonnie.blogspot.com
BonnieB  Monday, September 21, 2009 4:30 AM
The form itself is over 1500 lines and the designer file is over 3800 lines. When I try to post the code, I get aa message box and an empty triangle. I am assuming that the code is to big for the forum to take. I would be more than happy to zip up the entire project and send it your way via email.

However, I have an update to the issue. I tried building a smaller prject so I could upload the code here but the problem is not happening there. I created an Access database. Then created three tables, the main table for display and the two supporting tables. I created one relationship between the country and the region tables.

I have set up the relationship exactly between the Access database and the SQLExpress database. I even went so far as to delete the region combobox and related binding sources as well as the relationship in the SQL Server database itself ands rebuild them, however the problem persists.
cbGifford  Monday, September 21, 2009 6:16 AM
I didn't ask for the entire form, I asked for relevant code ...

1)The code you used to fill your ComboBoxes (did you set a .DataSource or fill themmanually).
2)The code you used to databind the ComboBoxes SelectedValue.
3) The code you used to "relate" the two Combos... IOW, I assume you have code in the SelectedIndexChanged event handler to do something to the second Combo when a selection is made from the first Combo.

Lastly, there is obviously something different between the way you did the Accessversion vs the SQL Express version. Maybe, if you can spot the differences, post that too.
~~Bonnie Berent [C# MVP]

(new blog --- not many posts yet --- be patient)
geek-goddess-bonnie.blogspot.com
BonnieB  Monday, September 21, 2009 3:05 PM

So what code do I send you from the form? I used the designer to generate the code.

The CountryComboBox bindingused is SelectedValue pointing to the column in my main tableAdapter. I set the DataSource to the lookup tableAdapter. I set the DispalyName to the lookup table column that holds the stringIwant displayed. I set the ValueMember to the lookup table related column. The RegionComboBox is set up exactly the same with one difference. I set the DataSource to the forienkey relationship between the region and country tables created in the database. Using the forienkey relationship as the binding source changes the underlyingdataTable for the region combobox when the country ComboBox SelectedValue changes, alleviating the need to write custom code in the SelectedIndexChanged of either ComboBox.

I have written no custom code to obtain this behavour between the two controls. If you can tell me where the relevant code is in over 3800 lines of code automatically generated by the IDE, I will be more than happy to send it to you. I set the Access project up EXACTLY the same way. And believe me, I stared at the two projects for hours trying to figure out what the difference was.

Hopefully I have explained a little better how I have set things up. I will continue to look into the designer code for the Access project to see where the difference is and will post it here if I can find it.

I have also added a datasource to the small access project that uses SQLExpress and duplicated the database I built in Access there as well. I also made the apropriate changes to the datasources in the designer. The comboboxs work fine there as well.

Again any help is much appreciated.

  • Edited bycbGifford Tuesday, September 22, 2009 12:20 AMAdditional Information
  •  
cbGifford  Monday, September 21, 2009 9:38 PM
Why don't you zip up both solutions separately and send both zip files to me. Other thanhow I've alreadymentioned above as to what code you'd need to show us, I can't think of another way of explaining that. Might be easier if I just look at it.

Send it to me at: bonnieberent AT gmail DOT com
~~Bonnie Berent [C# MVP]

(new blog --- not many posts yet --- be patient)
geek-goddess-bonnie.blogspot.com
BonnieB  Tuesday, September 22, 2009 12:30 AM
I apreciate this. I've sent the two projects zipped up for you. I added two more datasources to the Access project. One that duplicates the access database exactly, only in SQLExpress and another that uses the actual SQLExpress tables from my project. All the combo boxes in the test project work like they should.
cbGifford  Tuesday, September 22, 2009 12:51 AM
Ok, I solved the problem though I do not know what actually caused the issue in the first place. I deleted the comboboxes, then the binding sources and tableadapters from the form. Then I deleted the dataset itself. I recreated the dataset then the comboboxes and set the bindings and datasource as apropriate and the problem went away.

I can only assume that there was some type of corruption in the dataset designer files. Thanks again for the assistance.
  • Marked As Answer bycbGifford Tuesday, September 22, 2009 2:19 AM
  •  
cbGifford  Tuesday, September 22, 2009 2:19 AM
Sorry I couldn't help you out with all that Chris (sorry about the gmail problem) ... but I'm glad you got it all figured out on your own.

I am pretty certain that a good part of your "pain" with all this is the darn TableAdapters.

First, they really should not be in the UI and yet everyone sticks them in their forms, because that's what all the examples show. DataAccess should be separated from UI. Data should be passed around between layers(whether you use classes or DataSets to represent data, it doesn't matter).

Second, I've ranted against TableAdapters in other threads, here and on other forums. They are not Microsoft's best idea, IMHO. They arebasically just a wrapper around the DataAdapter functionality, supposedly to make things easier. I think they unnecessarily complicate things, which makes things harder, not easier.I recently (just this morning) posted a rant on my blog, in case anyone's interested.
~~Bonnie Berent [C# MVP]

(new blog --- not many posts yet --- be patient)
geek-goddess-bonnie.blogspot.com
BonnieB  Tuesday, September 22, 2009 4:21 PM

You can use google to search for other answers

Custom Search

More Threads

• Table Adapter Query with Parameterized Where Like Failure
• Datagrid: hange while scrolling
• Switching between databases
• refreshing datagrid
• Linking a combobox to a remote database
• Nested DataGrid
• Newbie needs help: Accessing a row of a datagrid (C#)
• Comparing fields
• Datagrid can´t load scrollbar after get data from webservice using Thread at Form.Shown
• Updating Dataset and Datagrid