We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date


I have bind the combobox using dataset. Displaymember and value member. My value member is integer and display member is string.In runtime i want to find the value of valuemember. My valuemember have 1,24,34,56,.. i am giving 56 and the corresponding value to be selected

1 Reply

CB Clay Burch Syncfusion Team April 13, 2002 04:15 AM UTC

This discussion assumes you have your combobox bound to a dataview. If you are using a datatable, then you can use the DefaultDataView member of the datatable as the DataSource for your combobox.
In this DataView case, the combobox's Items member is a collection of DataRowViews. We can use these DataRowViews to get the corresponding DisplayMember if we know the ValueMember, and vice versa.
The next comment is that the FindString and FindStringExact work only with the DisplayMember. So, they will not help you in looking for a ValueMember. Our solution is to add a public method to a derived combobox that does a 'FindStringExact' except that it looks at the ValueMember of the DataRowView instead of the DisplayMember. Below are some code snippets.
public class MyComboBox : ComboBox
	public int FindExactValueMember(object find)
		return FindExactValueMember(find, 0);

	public int FindExactValueMember(object find, int start)
		for (int i = start; i < this.Items.Count; ++i)
			DataRowView drv = this.Items[i] as DataRowView;
			if(drv != null &&  find.Equals(drv[this.ValueMember]))
				return i;
		return -1;

int index = comboBox1.FindExactValueMember(56); if(index > -1) comboBox1.SelectedIndex = index;

Live Chat Icon For mobile
Up arrow icon