Finding values in a databound combobox

scotter26

Newcomer
Joined
Dec 7, 2005
Messages
1
I have a set of two comboboxes. When the value of combo1 changes I need to change the selecteditem of combo2. The selecteditem of combo2 is related to the particular value selected in combo1.

The mechanics of this is easy enough but I'm having a problem.

Since the combobox (combo2) is databound, its items are datarowviews. This means I cannot use items.indexof to search for my value. combo2.FindString and combo2.FindStringExact will not work in this instance because I need to search by the value.

I can easily find the DataRow in the original DataSource.DataTable that the value is in but how do I convert it to a DataRowView which I can search on using indexof?

Or can someone think of a better way?

Thanks,
Scott
 
FindString will not work because you need to search for the value?

If you convert the value to a string, how is it that find string will not work?


Either way, you could create a mapping of the first combobox value to the second...

Im assuming that there is a one to many relationship between combo2 and combo1, as in there could be multiple items you can select in combo1 that will result in the same item being selected in combo2. If it's a one to one relationship then this whole thread is crap and you would have just ordered each list so that when you select item 5 in combo1, item5 is selected in combo2. If there is a one to many from combo1 to combo2, then I don't know how you will figure out which item is to be selected in combo2 when an item is selected in combo1.

Anyway, just create an array and populate the array with the index of the item to select in combo2 when that array index matches the index of the item selected in combo1.

C#:
			int[] itemMap = new int[this.comboBox1.Items.Count];

			itemMap[0] = 5;
			itemMap[1] = 2;
			itemMap[2] = 3;

There might be a better way, but hell if I know.
 
Back
Top