Okay, I've been researching this for a while, but I have yet to find an answer. Assume I have a List<Person> that has some properties on it: FirstName, LastName, Race, and Image. FirstName and LastName are STRINGS. Race is an ENUM. Finally, Image is of type System.Drawing.Image. I bind the List<Person> to a ListBox. I then proceed to bind the other properties to controls on the UI. I bind FirstName to a TextBox, LastName to a TextBox, Race to a ComboBox, and Image to a PictureBox.
BindingSource bs = new BindingSource();
bs.DataSource = this.lstPersons;
this.ListBoxPerson.DataSource = bs;
this.TextBoxFirstName.DataBindings.Add("Text", bs, "FirstName");
this.TextBoxLastName.DataBindings.Add("Text", bs, "LastName");
...
this.ComboBoxRace.DataBindings.Add("SelectedValue", bs, "Race");
this.PictureBoxImg.DataBindings.Add("Image", bs, "Image");
Here's my situation. All controls are populated upon startup. I change the selected person in the ComboBox.Now, when I do this, the RACE and IMAGE properties call the SET property,but the controls bound to the strings -- FirstName and LastName -- do not call SET. During this time, I have not changed any information for any of the bound controls. My question. Why do the controls bound to strings not call SET, but the controls, which are bound to my Enum or System.Drawing.Image, are calling their respective SET properties? Thank you, Trecius | | Trecius Tuesday, May 19, 2009 3:16 AM | Hi Trecius, The ComboBoxRace is bound to Race using simple list data binding, once you change the selectedValue of the ComboBox, it only changes the value of the Race of the current record, and don't change the current position. So the FirstName and LastName is not changed. If we want to make the ComboBox to synchronize with the TextBoxes and other controls, the ComboBox should use Complex data Binding. That means, we need to set the DataSource property of the ComboBox to bs, and set DisplayMember and ValueMember to decide which field in the datasource is to be displayed and which field needs to be considered as value. The following code demonstrates this.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
List<Person> lstPersons = new List<Person>();
lstPersons.Add(new Person("AA", "aa", Race.A, Properties.Resources.test));
lstPersons.Add(new Person("BB", "bb", Race.B, Properties.Resources.test));
BindingSource bs = new BindingSource();
bs.DataSource = lstPersons;
this.comboBox1.DataSource = bs;
this.comboBox1.DisplayMember = "FirstName";
this.textBox1.DataBindings.Add("Text", bs, "FirstName");
this.textBox2.DataBindings.Add("Text", bs, "LastName");
}
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = false;
}
}
public enum Race
{
A,
B
}
public class Person
{
string firstName;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
string lastName;
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
Race theRace;
public Race TheRace
{
get { return theRace; }
set { theRace = value; }
}
Image image;
public Image Image
{
get { return image; }
set { image = value; }
}
public Person()
{
}
public Person(string pFirstName,string pLastName, Race pTheRace, Image pImage)
{
firstName = pFirstName;
lastName = pLastName;
theRace = pTheRace;
pImage = image;
}
If you have anything unclear, please feel free to let me know. Best regards, Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't. - Marked As Answer byTrecius Friday, May 22, 2009 12:31 AM
-
| | Bruce.Zhou Wednesday, May 20, 2009 5:30 AM | Hi Trecius, The ComboBoxRace is bound to Race using simple list data binding, once you change the selectedValue of the ComboBox, it only changes the value of the Race of the current record, and don't change the current position. So the FirstName and LastName is not changed. If we want to make the ComboBox to synchronize with the TextBoxes and other controls, the ComboBox should use Complex data Binding. That means, we need to set the DataSource property of the ComboBox to bs, and set DisplayMember and ValueMember to decide which field in the datasource is to be displayed and which field needs to be considered as value. The following code demonstrates this.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
List<Person> lstPersons = new List<Person>();
lstPersons.Add(new Person("AA", "aa", Race.A, Properties.Resources.test));
lstPersons.Add(new Person("BB", "bb", Race.B, Properties.Resources.test));
BindingSource bs = new BindingSource();
bs.DataSource = lstPersons;
this.comboBox1.DataSource = bs;
this.comboBox1.DisplayMember = "FirstName";
this.textBox1.DataBindings.Add("Text", bs, "FirstName");
this.textBox2.DataBindings.Add("Text", bs, "LastName");
}
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = false;
}
}
public enum Race
{
A,
B
}
public class Person
{
string firstName;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
string lastName;
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
Race theRace;
public Race TheRace
{
get { return theRace; }
set { theRace = value; }
}
Image image;
public Image Image
{
get { return image; }
set { image = value; }
}
public Person()
{
}
public Person(string pFirstName,string pLastName, Race pTheRace, Image pImage)
{
firstName = pFirstName;
lastName = pLastName;
theRace = pTheRace;
pImage = image;
}
If you have anything unclear, please feel free to let me know. Best regards, Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't. - Marked As Answer byTrecius Friday, May 22, 2009 12:31 AM
-
| | Bruce.Zhou Wednesday, May 20, 2009 5:30 AM |
|