Z
Zac Maclean
I am using Databinding to fill a Combobox Display and Value members with
info from a table.
My problem (below code) happens twice. I call the following code once when
the form loads.
Form description:
Combobox loads with form (and after SQLCE sync) from .sdf file
Treeview populates based on item selected from combobox.
more happens after this, but I'm confident I have that handled.
------------ Code -------------------
cmbLot.Items.Clear();
string cmbstr = "SELECT ColOne + ' - ' + ColTwo as Display, PK FROM TableOne
ORDER BY Display";
try
{
SqlCeConnection connCE = new SqlCeConnection("Data Source=" +
strCEDatafile);
SqlCeDataAdapter daNames = new SqlCeDataAdapter(cmbstr, connCE);
try
{
DataSet dsConnect = new DataSet();
DataTable Combo = new DataTable("Combo");
connCE.Open();
dsConnect.Tables.Add(Combo);
daNames .Fill(dsConnect, "Combo");
cmbLot.DataBindings.Add("SelectedItem", Combo, "Display");
cmbLot.DataBindings.Add("SelectedValue", Combo, "PK");
cmbLot.DataSource = Combo;
cmbLot.DisplayMember = "LotNum";
cmbLot.ValueMember = "PK";
connCE.Close();
}
catch(SqlCeException sqlex)
{
string sqlCEerror = sqlex.Message.ToString();
}
}
catch (Exception ex)
{
clsErrors.WriteEx(ex);
MessageBox.Show("Error reading data file.\nContact Helpdesk", "Combobox
Error");
}
------------ Code -------------------
Then I am waiting for the user to select from the items that populate this
Combobox. The problem is that while loading this the OnSelectedIndexChanged
event fires 4 times (index of 0). (I'm loading 23 items in my test
environment). Then..... once I select an item, it fires multiple times
again. This causes problems with the code I want to run when a new item is
selected. It runs once ok, but it runs 4 times. Its almost like it is
reloading the databinding every time I fire any event on the form.
I've traced the event, the "index" change is first, the selected index, then
0, then the selected index, then 0 again.
I have used dynamic fills on comboboxes without problems before, but not
databinding.
TIA for any help
Z
info from a table.
My problem (below code) happens twice. I call the following code once when
the form loads.
Form description:
Combobox loads with form (and after SQLCE sync) from .sdf file
Treeview populates based on item selected from combobox.
more happens after this, but I'm confident I have that handled.
------------ Code -------------------
cmbLot.Items.Clear();
string cmbstr = "SELECT ColOne + ' - ' + ColTwo as Display, PK FROM TableOne
ORDER BY Display";
try
{
SqlCeConnection connCE = new SqlCeConnection("Data Source=" +
strCEDatafile);
SqlCeDataAdapter daNames = new SqlCeDataAdapter(cmbstr, connCE);
try
{
DataSet dsConnect = new DataSet();
DataTable Combo = new DataTable("Combo");
connCE.Open();
dsConnect.Tables.Add(Combo);
daNames .Fill(dsConnect, "Combo");
cmbLot.DataBindings.Add("SelectedItem", Combo, "Display");
cmbLot.DataBindings.Add("SelectedValue", Combo, "PK");
cmbLot.DataSource = Combo;
cmbLot.DisplayMember = "LotNum";
cmbLot.ValueMember = "PK";
connCE.Close();
}
catch(SqlCeException sqlex)
{
string sqlCEerror = sqlex.Message.ToString();
}
}
catch (Exception ex)
{
clsErrors.WriteEx(ex);
MessageBox.Show("Error reading data file.\nContact Helpdesk", "Combobox
Error");
}
------------ Code -------------------
Then I am waiting for the user to select from the items that populate this
Combobox. The problem is that while loading this the OnSelectedIndexChanged
event fires 4 times (index of 0). (I'm loading 23 items in my test
environment). Then..... once I select an item, it fires multiple times
again. This causes problems with the code I want to run when a new item is
selected. It runs once ok, but it runs 4 times. Its almost like it is
reloading the databinding every time I fire any event on the form.
I've traced the event, the "index" change is first, the selected index, then
0, then the selected index, then 0 again.
I have used dynamic fills on comboboxes without problems before, but not
databinding.
TIA for any help
Z