473,574 Members | 5,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Val ueField1";
this.ComboBox1. FormattingEnabl ed = 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.F ill(this.Datase t1.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 6703
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.BindingCon text[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.comwro te 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.Val ueField1";
this.ComboBox1. FormattingEnabl ed = 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.F ill(this.Datase t1.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.comskr ev i melding
news:%2******** ********@TK2MSF TNGP04.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
4521
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 index is set to the proper item. However, when accessing its selectedindex later it has reverted to the previous value. The selectedindex value is...
1
3002
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 help.. thanks.
3
16202
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; comboBox.SelectedIndex = -1; ... }
2
2224
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 comes up correctly as blank. However, if I change the filter, and set the selected index to -1, is always shows the first record from the dataview...
1
2403
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 sets the visible property. When the visible property is set.. 'Petition Type lblPetitionType.Visible = False
4
2604
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 property with 2000 Items listed in the ComboBox, SelectIndex property accepts assigned value. However, when I try to use SelectIndex
9
4639
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 about? For i = 0 To cboCalc.Items.Count - 1 Dim Item As FileInfoExt Item = cboCalc.Items(i) If Item.TruncatedFileName = DefaultName Then...
1
1574
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 showed up, even if I set the SelectedIndex of my ComboBox to -1, the first value in my collection is selected. I check everywhere in my code to be...
5
6883
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
7821
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7739
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8083
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6487
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5326
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3777
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2255
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1087
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.