473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ComboBox SelectedIndex Changes after fill

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
3 6685
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ross kerr | last post by:
Hi all, I have a control that extends the ComboBox object. It updates the selected item based on what the user enters in the text area. In the OnLeave event of the combobox, the selected...
1
by: jim | last post by:
How do I immediately set the SelectedIndex of a combobox (dropdownlist) upon Form Load? I'm getting out of range errors and I suspect it's due that the Form has not been displayed yet. Please...
3
by: ScottO | last post by:
I would like the user to have to select something in a System.Windows.Forms.ComboBox. private void MyForm_Load(object sender, System.EventArgs e) { ... comboBox.DataSource = data;...
2
by: Vern | last post by:
I have a combobox called sPropertyCountyCd whose datasource is a dataview. The first time I set the filter to the dataview, attach the combobox to the dataview, and set the selected index to -1 is...
1
by: mike | last post by:
Hi, I'd like advice from a .NETer. I have a loadcombo routine which sets the selectedindex to -1 if it's an "add record", but then it goes to a security routine which, based on permissions...
4
by: Strahimir Antoljak | last post by:
Has anyone experienced problems with a combo box SelectIndex property? Is there a limit to the number of Items for a combo box? Namely, when I set programmatically ComboBox.SelectIndex...
9
by: Atchoum | last post by:
I have a combobox populated with objects from a class derived from FileInfo. When I try to assign a value to selected index, I get a "Argument length must be greater of equal to 0". What is this...
1
by: Marc Robitaille | last post by:
Hello, I fill a ComboBox with a collection on my own. So I set the DisplayMember and the ValueMember properties to some fields of my collection. It works perfectly. The problem is when my form...
5
by: Rotsey | last post by:
Hi, I have a combobox that when I set the SelectedIndex to -1 it sets to 0. The combobox Items property says there is 20 items in it. Anyone know why this could be please? rotsey
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.