472,353 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 6554
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...
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...
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)...
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...
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...
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...
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...
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....
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. ...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.