By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,686 Members | 2,589 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,686 IT Pros & Developers. It's quick & easy.

ComboBox SelectedIndex Changes after fill

P: n/a
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
Sep 28 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
--
HTH
Stoitcho Goutsev (100)

"Magnus" <no@spam.comwrote in message news:45********@news.broadpark.no...
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


Sep 28 '06 #2

P: n/a

"Stoitcho Goutsev (100)" <10*@100.comskrev i melding
news:%2****************@TK2MSFTNGP04.phx.gbl...
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
Sep 28 '06 #3

P: n/a
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????
--
VB.net is good
"Magnus" wrote:
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
Dec 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.