Databiinding to Combobox

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
 
Z

Zac Maclean

Found my issue..

Removed the following from the posted code, and it stopped the multi-fire
when something was selected (but not during initial loads... not a problem
for me)

Removed:
 
Z

Zac Maclean

Need to Not hold Ctrl-key down so much....

Found my issue..

Removed the following from the posted code, and it stopped the multi-fire
when something was selected (but not during initial loads... not a problem
for me)

Removed Code:
cmbLot.DataBindings.Add("SelectedItem", Combo, "Display");
cmbLot.DataBindings.Add("SelectedValue", Combo, "PK");

I guess if you use the Datsource / Displaymember / Valuemember, you don't
need to Databind them.

Which leads on to another post for tomorrow about DataBinding. :-/

Z
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top