2 dropdownlists an a problem

  • Thread starter Thread starter rodchar
  • Start date Start date
R

rodchar

hey all,
i have 2 dropdownlists that are the same but are different

they both have first name and last for the text values, however, in list the
names are all capitalized and shown as last name first name and in the other
list it's first name last name Proper case.

Another difference is the names have different value for the values in the
list.

is there anyway to take the text value of one list and find it in the other
list given the context?

thanks,
rodchar
 
i have 2 dropdownlists that are the same but are different

they both have first name and last for the text values, however, in list
the
names are all capitalized and shown as last name first name and in the
other
list it's first name last name Proper case.

Another difference is the names have different value for the values in the
list.

is there anyway to take the text value of one list and find it in the
other
list given the context?

ComboBox.FindString()
 
In addition to Jeff's solution, if both ComboBoxes have the same datasource
you can automatically select the other item using databinding.

This code sample demonstrates how you can have a combobox displaying the
FirstName and another displaying the LastName and selecting in either
ComboBox would cause the other ComboBox to get updated (BindingSource handles
the syncronization)

public partial class Form1 : Form
{
BindingSource bs = new BindingSource();
ComboBox combo1 = new ComboBox();
ComboBox combo2 = new ComboBox();
public Form1()
{
Controls.Add(combo1);
combo2.Left = combo1.Right + 5;
Controls.Add(combo2);
}

protected override void OnLoad(EventArgs e)
{
List<Person> people = new List<Person>();
people.Add(new Person { FirstName = "Max", LastName = "Manus" });
people.Add(new Person { FirstName = "Smith", LastName =
"N'Jones" });
people.Add(new Person { FirstName = "Henry", LastName = "Rex" });

bs.DataSource = people;
combo1.DataSource = bs;
combo1.DisplayMember = "FirstName";
combo2.DataSource = bs;
combo2.DisplayMember = "LastName";
}

class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}

I suspect you have something like a list of Person in one ComboBox and a
list of Employee in another. They might be the same people but their names
are displayed differently and the lists are different (ie may not have
exactly the same peole). In that case, if you can figure out how the name is
displayed in the other ComboBox you can use FindString like Jeff pointed out.
The best solution would be to do it behind the scenes using a unique
identifier between the list, like a social security number. This would
support cases where you have two people with the same name.
 
In addition to Jeff's solution, if both ComboBoxes have the same datasource
you can automatically select the other item using databinding.

This code sample demonstrates how you can have a combobox displaying the
FirstName and another displaying the LastName and selecting in either
ComboBox would cause the other ComboBox to get updated (BindingSource handles
the syncronization)

public partial class Form1 : Form
{
BindingSource bs = new BindingSource();
ComboBox combo1 = new ComboBox();
ComboBox combo2 = new ComboBox();
public Form1()
{
Controls.Add(combo1);
combo2.Left = combo1.Right + 5;
Controls.Add(combo2);
}

protected override void OnLoad(EventArgs e)
{
List<Person> people = new List<Person>();
people.Add(new Person { FirstName = "Max", LastName = "Manus" });
people.Add(new Person { FirstName = "Smith", LastName =
"N'Jones" });
people.Add(new Person { FirstName = "Henry", LastName = "Rex" });

bs.DataSource = people;
combo1.DataSource = bs;
combo1.DisplayMember = "FirstName";
combo2.DataSource = bs;
combo2.DisplayMember = "LastName";
}

class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}

I suspect you have something like a list of Person in one ComboBox and a
list of Employee in another. They might be the same people but their names
are displayed differently and the lists are different (ie may not have
exactly the same peole). In that case, if you can figure out how the name is
displayed in the other ComboBox you can use FindString like Jeff pointed out.
The best solution would be to do it behind the scenes using a unique
identifier between the list, like a social security number. This would
support cases where you have two people with the same name.
 
Back
Top