I am totally mystified by the workings of the combobox in the .Net environment. My problem is similar to that described by Johnathan on 25th Jan 2006 and replied to by Mattias Sjogren. Whilst Johnathan's original attempt was not the solution he used the following statement
"foreach (String item in comboBoxCatagory.Items)"
In order to try and get and understanding of how the Items are used I tried to execute the following:
"foreach
(String str in SectorComboBox.Items)" and got the following error:Unable to cast object of type 'System.Data.DataRowView' to type 'System.String'.
I have been attempting to set the SectorComboBox.SelectedIndex to a value just as Johnathan had been and got very strange results - the underlying dataset table was having its records changed which in turn screwed up the combobox selections.
The recommended solution for Johnathan was
comboBoxCatagory.SelectedItem = item;
I have tried setting my SelectedItem to a string value and whilst the statement executes it doesn't have the effect I want.
My scenario is:
I have two tables - Investment and Sector. The investment record contains a lot of data and so instead of having a user edit investment data directly from rows in a DataGridView I have a form which enables a user to select an investment from a DataGridView which only contains a Investment Code and an Investment Description.That selection populates a whole lot of textboxes, comboboxes, etc on a number of TabPages.
One of these Comboboxes is bound to the Sector table. This combobox is initialised with the sector value corresponding to a sector code maintained in the Investment table. My problem is that as a user selects different investments from the DataGridView, the Sector descriptions in combobox get out of Wack! I have traced this down to dataset records getting screwed up by my attempts at setting the SelectedIndex.
Can anyone straighten me out Reference to a document or a text that describes the binding operation particularly with reference to comboboxes would be appreciated.
Thanks

Combobox Databindings
jmead3
The items in a combobox does not need to be of String class. What you get displayed is the ToString() value of the object stored in the item collections.
In your case you get the unable to cast error because databinding actually stored the DataRowView object in it and foreach requires that it can be casted to the target type, in your example String.
You can iterate through them all by
foreach (DataRowView drv in SectorComboBox.Items)
In MSDN you see ComboBox.Items property gives an ObjectCollection.
I hope this helps you a bit further.
nezoat
Thanks Andreas.
I've now also discovered that the bindingSource object has all the necessary methods and properties to be able to select what I want displayed in the combobox.
It is hard yakka trying to figure out how databinding actually works. I still haven't found any articles that explain it clearly from first principles.