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.
Unfortunately, activation email could not send to your email. Please try again.

Comboboxbinding

Thread ID:

Created:

Updated:

Platform:

Replies:

171 Apr 11,2002 03:34 PM Apr 13,2002 12:15 AM Windows Forms 1
loading
Tags: FAQ
Srini
Asked On April 11, 2002 03:34 PM

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

Clay Burch [Syncfusion]
Replied On April 13, 2002 12:15 AM

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.
Clay
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;
	}
}

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

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;