0

I have these classes:

public class City { 
public string name { get; set; } 
public override string ToString() { return name; } 
}
public List<City> cities = new List();
public class Person {
public string name { get; set; }
public string addr { get; set; }
public City city { get; set; }
}
List<Person> persons;

I bind cities and persons to bindingSource (and to list and textbox):

bindingSource_city.DataSource = cities;
textBox1.DataBindings.Add("Text", bindingSource_city, "name");
bindingSource_person.DataSource = person;
textBox2.DataBindings.Add("Text", bindingSource_person, "name");
textBox3.DataBindings.Add("Text", bindingSource_person, "addr");

also listBox.DataSource = bindingSource (in form editor).

All seems to work fine. How can I create combobox with cities in list and binded to person.city property? I cant understand how to fill drop-down list with objects (not string items).

EDIT: Or how to convert person.city to SelectedIndex on data (list index) change and back.

ice1000
  • 6,406
  • 4
  • 39
  • 85
Alexey
  • 593
  • 1
  • 5
  • 14
  • Thank, but its not an answer. In you example combobox binded to one bindingSource and in my - to two. – Alexey Jun 25 '16 at 12:57

2 Answers2

0

It is very simple to do:

comboBoxCities.DataSource = bindingSource1;
comboBoxCities.DisplayMember = "name";
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
  • How its binded to person.city? I can fill drop-down list, but i cant understand how to bind it to property. – Alexey Jun 25 '16 at 12:54
  • I try to manually update data in list, and its works fine. Also I try to use you code. It has side effect - when i change cursor (SelectedIndex) in comboBox, cursor in first listBox also changed :) – Alexey Jun 25 '16 at 13:11
-1

Partially solved by adding events:

bindingSouce_city_ListChanges
{
comboBox1.Items.Clear();
foreach( City c in cities ) comboBox1.Items.Add( c );
}
comboBox1_SelectedIndexChanged(...)
{
int i = comboBox1.SelectedIndex;
((Person) bindigSource_person).city = (i < ? null: cities[ i ]);
}
bindingSource_person_CurrentChanged(...)
{
comboBox1.SelectedIndex = 
cities.IndexOf( ((Person) bindingSource_person).city );
}

Its not very nice code, I think, but its work.

UPD: Thanks to eugene-podskal Solution is:

comboBox1.DataSource = bindinsSource_city;
comboBox1.DataBinding.Add( "SelectedItem", bindingSource_person, "city" );
Community
  • 1
  • 1
Alexey
  • 593
  • 1
  • 5
  • 14
  • Have you tried just to add a binding on SelectedItem to your person's city? Like in http://stackoverflow.com/questions/31343543/winforms-binding-a-combobox-selecteditem-to-an-object-property? – Eugene Podskal Jun 25 '16 at 13:20
  • Yes! Thanks! Its what I want! – Alexey Jun 25 '16 at 13:29
  • Do I need to update my answer or question for proper solution? – Alexey Jun 25 '16 at 13:31
  • Answers are for answers, questions are for questions, so you should update the answer. Just make a more specific title for your question (the current one doesn't fully describe the issue - I'd recommend something along the line of 'Binding ComboBox's DataSource to one object and SelectedItem to another '), and add `[winforms]` [tag](http://stackoverflow.com/help/tagging) to it. – Eugene Podskal Jun 25 '16 at 14:12