ComboBox SelectedIndex Changes after fill

M

Magnus

Im using a set combobox (ComboBox1) to provide a selection of records from a
database table. I have a typed dataset (DataSet1) that contains the typed
datatable (DataTable1) that the combobox is bound to. The datatable in the
dataset is filled using the typed tableadapter (TableAdapter1). DataTable
consists of the typical primary key and value fields.

Here is the designer generated code for ComboBox1:
this.ComboBox1.DataSource = this.Dataset1;
this.ComboBox1.DisplayMember = "DataTable1.ValueField1";
this.ComboBox1.FormattingEnabled = true;
this.ComboBox1.Location = new System.Drawing.Point(77, 39);
this.ComboBox1.Name = "ComboBox1";
this.ComboBox1.Size = new System.Drawing.Size(303, 21);
this.ComboBox1.TabIndex = 6;

This is the forms load code:
TableAdapter1.Fill(this.Dataset1.DataTable1);

What I have problems with is that the ComboBox1, when filled in the forms
Load event code, gets it's SelectedIndex changed from -1 to 0, i.e. first
option is already selected when the form is loaded. I do not want this
behaviour and wonder what is causing it, and to me it just seems random and
unpredictable.

I have not included the rest of the forms code and settings, as I think
there is nothing out of the ordinary, I just hope someone recognizes the
condition and can explain what is causing it and how you can control it. Can
anyone shed som light on this? I understand I can just set the SelectedIndex
by code after filling the DataTable, but it's an ugly solution.

Thank you.

- Magnus
 
S

Stoitcho Goutsev \(100\)

Magnus,

this is how the databinding works. When you set the DataSource a
CurrencyManager is created for this source. One of the tasks of the currency
manager is to track the currenetly selected item. This way if more than one
controls are bound to the same datasource changing the selected (current) in
one of them will be reflected by all controls. In your case if you bind for
example ListBox to the same data table chanaging selection the combobx will
change the selection in the listbox and vice versa. At the very end this is
what databinding is all about - keeping everything insync. When you
initially databind to a data source the current item is 0 and that cause the
combobox to change its selected item. There has to be always one current
item, thus you can force the SelectedIndex for the combobox to be -1, but
this won't change the current item for the datasource and this won't update
the rest of the bound controls.

To get the currency manager for a datasource you can do
CurrencyManager cm = this.BindingContext[arr] as CurrencyManager;

You can use the BindingContext on any control on the form. CurrencyManager
provides methods, properties and events that can be used to check currently
selected item, changed, subscribe for notifications in one place instead of
hooking many controls etc.
 
M

Magnus

Stoitcho Goutsev (100) said:
Magnus,

this is how the databinding works. When you set the DataSource a
CurrencyManager is created for this source. One of the tasks of the
currency manager is to track the currenetly selected item. This way if
more than one controls are bound to the same datasource changing the
selected (current) in one of them will be reflected by all controls. In
your case if you bind for example ListBox to the same data table chanaging
selection the combobx will change the selection in the listbox and vice
versa. At the very end this is what databinding is all about - keeping
everything insync. When you initially databind to a data source the
current item is 0 and that cause the combobox to change its selected item.

I use several combobox controls in other forms, their datasource property is
set to a lookup typed DataTable. They don't have any initial selected item,
which is natural. Imagine you open a form, and you have a set of combobox
controls, if you find that the combobox controls have text in them, you will
naturally think that an item is selected. Which might not be what you want,
i.e. you dont want the first option in the combobox control to be selected.
I want all of the lookup values to be default to not selected, a selection
should be by a users initiative, and not default to first item because
currencymanager just behaves like that. This way, if the user wants to make
a selection in the list, he will do so, if not, it will remain blank and no
items selected.

Obviously I must be doing something wrong, but I just dont get it. Right now
things are just magic, sometimes it goes, sometimes it doesen't.

Hope I made myself clear.

- Magnus
 
G

Guest

Magnus, I agree with you 100%, I have the same crazy problem which for me
doesn't makes sense, or may be we have to override something in the
CurrencyManager to stop this

Infact, if there is nothing selected in the combo, I want a
"****SELECT TEXT****" texst coming in the combo indicating to user that a
selection is required, i need the solution for this very immediately, if
some1 can help????
 

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