Hi,
I think there's a bug in the ComoBox when binding to a DataRelation.
The problem is that SelectedValueChanged event is not always being
called.
Here is some code to reproduce the problem. Add 2 comboboxes, 2 labels
and a timer to a form. Then add the code at the end of this message.
You will see that, when you select a new item in the child combobox,
the SelectedValueChanged event gets fired, but when you change the
parent combo it does not even though the child combo's selected item
does change.
Maybe I'm not using these controls in the 'right' way...BUT it shows
that, internally, the SelectedValue property _can_ be changed without
the SelectedValueChanged event firing (that's the purpose of the timer
- to prove the value does get updated).
Not a flame, just a polite request to MS to fix =o)
Thanks,
Paul
P.S just updated to Framework 1.1 SP1 today, problem still exists. I'm
guessing fixing this might also cause people problems who have already
coded round it...all of a sudden their events will be firing twice.
Me, I'd prefer to see it working properly and go through removing my
workarounds, just my opinion.
-----------------CODE SNIPPET---------------
int _iCount = 0;
private void Form1_Load(object sender, System.EventArgs e)
{
DataSet dsTemp = new DataSet();
DataTable dtParent = new DataTable("Parent");
DataTable dtChild = new DataTable("Child");
dtParent.Columns.Add("id", typeof(int));
dtParent.Columns.Add("sName", typeof(string));
dtChild.Columns.Add("id", typeof(int));
dtChild.Columns.Add("parentid", typeof(int));
dtChild.Columns.Add("sName", typeof(string));
dtParent.Rows.Add(new object[]{1, "Fruit"});
dtParent.Rows.Add(new object[]{2, "Veg"});
dtChild.Rows.Add(new object[] {1, 1, "Apple"});
dtChild.Rows.Add(new object[] {2, 1, "Pear"});
dtChild.Rows.Add(new object[] {3, 1, "Peach"});
dtChild.Rows.Add(new object[] {4, 2, "Carrot"});
dtChild.Rows.Add(new object[] {5, 2, "Parsnip"});
dtChild.Rows.Add(new object[] {6, 2, "Peas"});
dsTemp.Tables.Add(dtParent);
dsTemp.Tables.Add(dtChild);
DataRelation dRel = dsTemp.Relations.Add("Parent_To_Child",
dtParent.Columns["id"], dtChild.Columns["parentid"], true);
comboBox1.ValueMember = "Parent.id";
comboBox1.DisplayMember = "Parent.sName";
comboBox2.ValueMember = "Parent.Parent_To_Child.id";
comboBox2.DisplayMember = "Parent.Parent_To_Child.sName";
comboBox1.DataSource =
comboBox2.DataSource =
dsTemp;
}
private void comboBox2_SelectedValueChanged(object sender,
System.EventArgs e)
{
_iCount++;
label1.Text = "Events : " + _iCount;
}
private void timer1_Tick(object sender, System.EventArgs e)
{
label2.Text = "comboBox2.SelectValue = " +
((comboBox2.SelectedValue == null) ? "<Null>" :
Convert.ToString(comboBox2.SelectedValue));
}
}