ComboBox bug or am I missing something??

ScoobyChris

Member
Joined
Sep 3, 2006
Messages
6
Programming Experience
1-3
Hi all,

I am using the following code to update two items of data in a dataset...

ds.Tables("addresses").Rows(ComboBox1.SelectedIndex).Item("Street") = txt_street.Text
ds.Tables("addresses").Rows(ComboBox1.SelectedIndex).Item("Town") = txt_town.Text

This seems to work fine however the problem I have is that in reading the(ComboBox1.SelectedIndex) value this also seem to generate a
ComboBox1_SelectedValueChanged event. Is this normal? I'm not actually changing the index value so why is the change event triggered?

This wouldn't be a problem however one of the things the change event sub does is txt_town.Text.Clear() and hence the above code just gives me a blank town field in the dataset!! :)

Thanks in advance for any comments.
 
EDIT; I didnt notice I was in ASP.NET forum, and I dont have experience withy doing the procedure I describe below, in ASP.NET - only WinForms. Some advice may be incorrect. If it is, please let me know and i'll delete the post. If the procedure would be roughly the same for both, then this advice may apply:



I'm a little confused about how you have arranged your data here.Assuming you have a combobox that is used to select which item in a list to look at, and some text boxes that are used to edit data associated with the list then i'd have expected the structure to be:

datatable of N rows
bindingsource attached to the datatable (maintains knowledge of list position)
combobox bound to some column
textboxes bound to other columns


now, when the combobox selection is changed, the list is scrolled in the bindingsource to the relevant position. this will cause all the text boxes to update their contents, you can edit the contents and save the changes back to the table.
 
Hi,

I probably didn't explain it that well to be honest. I do have a combobox "combobox1" with it's datasource bound to a dataset column. What I'm actually doing when this code runs is updating the rows in the dataset using the selectedindex value of the combobox to select the row in the dataset.

The code is only using the value to index to the correct row in the dataset and should not be altering the selectedindex value of the combobox in any way. That's why I can't understand why the combobox's selectedindexchanged event is kicking off as the first line of code runs.

Any ideas?
 
Minor corrections:

DataSets do not contain any data directly. DataTables contain data, DataSets contain one or more DataTables.. Hence you cannot bind a combobox to a DataSet's column, nor can you select a row in a DataSet; they have none. In understanding your post I have substituted (in my mind) the word DataSet for DataTable.

I'm not sure why the event is firing either, but there is another way to bind your data that may help. Additionally, we have had problems with list controls like ListView having SelectedIndex events fire twice for no apparent reason.

On the form with your controls, add a BindingSource. Set the BindingSource DataSource member to be your dataset. Set the BindingSource DataMember to be a string of the name of the DataTable in the DataSet to which you wish to bind.

Now bind the combobox DataSource to the BindingSource, set the DisplayMember and ValueMember properties to be the column to be shown. If this combobox is to be editable, the Text property must also be bound so that typed chages in the ComboBox are propagated to the underlying DataTable. Otherwise if it is simply to be a drop down list, the .Text property neednt be bound.
For the controls, bind their .Text property (or equivalent e.g. .Value property for DateTimePicker) to the BindingSource.

The BindingSource maintains position information. When scrolled, all bound controls have their position changed. I will post an example project highlighting what I mean.
 
Last edited:
Here is the example project. Note that in this project I did everything using the visual designer. If you want to see the code going on behind the scenes that implements what I wrote above (about bindingsource etc), take a look at the Form1.Designer.vb file
 

Attachments

  • WindowsApplication2.zip
    24.8 KB · Views: 17
Hi Cjard,

Thanks for the reply and the sample application you posted. Yes your quite correct about the dataset / datatable thing. I'm very new to VB.NET and I've thrown myself in at the deep end a bit writing my first application full of dataset tables, database integration and god only knows how many control types! :) Obviously the datatable I am refering to in this post is "addresses" which contains the columns "Street" and "Town". This table is part of the "ds" dataset.

I have actually got around the problem now in a rather more simple or crude way if you like. I simply disable the combobox change even just before I execute those two lines of code and then re-enable it just after (just using a boolean variable) This prevents the event from clearing the txt_town textbox and hence allows me to successfully perform a dataadapter.update to my database.

It still begs the question of why does using the combobox.selectedindex value in this way cause the change event to fire in the first place though?
 
I dont know right now, but I may get chance to look at it later. It *may* be something to do with focus, actually.. I've found in the past that some controls, like radio buttons, will fire their events if they are focused, but not if they are not focused.

If you are using .NET 2, you may consider using typed datasets and tableadapters rather than your current course. Googling for DATA WALKTHROUGHS brings up a good site from microsoft with some handy tips on how to do database things in .net 2
 
Back
Top