combobox bound to data and edit form

bones

Well-known member
Joined
Aug 23, 2014
Messages
143
Programming Experience
Beginner
On my main form I have a combobox that is bound to data in an access.mdb.

The combobox displays a list of items in the database.

I use another form to add / edit the items displayed in the combobox on the main form.

After editing / adding an item, the combobox on the main form does not display the changes made. If I end debug and restart debug, then naturally the changes do appear but that's not a solution...

Main form load code
VB.NET:
Me.OrificedataTableAdapter.Fill(Me.ProFlow1DataSet.orificedata)

I added
VB.NET:
Private Sub cboFlowRanges_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFlowRanges.SelectedIndexChanged
Me.OrificedataTableAdapter.getdata()
end sub

It has no effect on the situation.

I searched for a solution and ran into all sorts of similar issues with datagridview refresh problems and all sorts of convoluted solutions, none of which seemed workable...

Advice needed...please
 
There's nothing convoluted about the solution to this problem. The issue is always the same and the best solution is always the same. The fact that you can't see it is in part due to the fact that you are looking at it the wrong way. This:
On my main form I have a combobox that is bound to data in an access.mdb.
Your ComboBox is not bound to any database. The ComboBox knows nothing about any database. The ComboBox is bound to a DataTable. If you expect the contents of the ComboBox to change then the contents of that DataTable has to change. Changing what's in the database isn't going to do that because there is no connection between the database and the DataTable. As for this:
VB.NET:
Me.OrificedataTableAdapter.getdata()
you obvioulsy don't know what that does or it would be obvious why it has no effect. A table adapter opens a connection to a database, executes a query and populates a DataTable with the result set. If you call Fill on the table adapter, you pass an existing DataTable and it gets populated. If you call GetData, it creates a new DataTable, populates it and returns it to you. You're calling GetData, so that's not going to affect the DataTable full of data you already have and you're not using the DataTable that it returns. That's like having a half-full cereal box, sending someone to the supermarket to get more cereal, throwing away the box they bought and then wondering why your cereal box is still only half-full.

If you already have a populated DataTable and you want to make changes then you make changes to the DataTable FIRST, THEN save the changes to the database. You do NOT save the changes to the database first and then worry about how to get that change into your DataTable. So, your editing form should not be doing anything related to the database. Your editing form should simply receive an existing value and pass you back a new value or just pass you back a new value when editing. Your main form should then modify the DataTable with that change. Because the ComboBox is bound to the DataTable, it updates right there and then, even though the database has not been changed yet. You then use the same table adapter that retrieved the data in the first place to save the changes back to the database.
 
If you already have a populated DataTable and you want to make changes then you make changes to the DataTable FIRST, THEN save the changes to the database.

I guess you don't realize that has already been done on the editing form.


You do NOT save the changes to the database first and then worry about how to get that change into your DataTable. So, your editing form should not be doing anything related to the database.

This makes no sense at all... The editing form is the SOLE editing connection to the database. I have no idea what your thinking is here...

Your editing form should simply receive an existing value and pass you back a new value or just pass you back a new value when editing.

Correct...even though it is contrary to your statement above....


Your main form should then modify the DataTable with that change.

No...absolutely incorrect. The combobox on the main form is NEVER used to modify the datatable. It is simply a data retrieval tool and it's not getting updated...THAT is the issue I' trying to resolve.


Because the ComboBox is bound to the DataTable, it updates right there and then, even though the database has not been changed yet. You then use the same table adapter that retrieved the data in the first place to save the changes back to the database.

Not exactly...

Form1 - contains combobox that displays bound data. It has but one purpose... Display the current data that exists in the datatable.

Form2 - contains DGV. DGV retrieves from and updates the .mdb using .update code when a new item is added or an existing item is modified.

Form1.combobox initially fills on form load using the code I originally posted.


If on form2, the DGV is used to edit or create a new data item, there is a save button that runs the update code.

On form1, the combobox does not know there has been a change to the datatable and/or the DB [even thought the DB isn't really relevant to the cbobox..I get that]

SO THE MISSION, SEEMINGLY SIMPLE...was to code the combobox using "SelectedIndedChanged" to refill the combox. Getdata seemed the right choice. I now see why it didn't work. Thanks for that.
 
Last edited:
I was assuming that your add/edit form was just editing one item at a time but I now see that that's not the case. Your issue is that you have two different DataTables. They each contain a copy of the data in the database but they are both disconnected from that database. Making changes to one has no affect on the other and making changes to the database has no effect on either. Think of it this way: if I write your age on two pieces of paper, does rubbing one out and changing it affect the other or are either affected when your next birthday comes around?

So, you have two choices:

1. Use a single DataTable. If you only have one DataTable then any changes you make to it will be reflected anywhere that one DataTable is used. Presumably Form1 is the first displayed, so it should create and populate the DataTable. When Form2 is displayed, instead of creating its own DataTable, Form1 should pass the existing DataTable to it. Now, any changes that Form2 makes to the DataTable will be reflected in Form1 because it's bound to the same DataTable.

2. Use two DataTables but refresh the contents of the first whenever changes have, or may have, been made to the second. Presumably Form1 is displaying Form2. When Form2 closes, Form1 can clear its DataTable and repopulate it from the database. That way, it will always contain the current data.

Neither option is convoluted at all.
 
Back
Top