1

I have a small issue that I am unable to figure out. I have a form in my application called FORM1. On button click SEARCH - i show user data in a DATAGRIDVIEW. So now that I have data - user has the option to add a new client, so they click a button and I have a new form that pops up. I do not close FORM1 - i leave it as it and create a new child like this....

Dim NewMDIChild As New frmNewClient()
NewMDIChild.MdiParent = MDIContainer
NewMDIChild.Show()
MDIContainer.Show()

Now user will be able to enter data and save it on the frmNEWCLIENT. Once they are done, they click a button called DONE which brings them back to FORM1. And here I want the grid to be refreshed so that it includes the new client as well. But I don't know how!

I tried doing it in Activate Event of the form - But in that case - the grid gets loaded when I initially open the FORM1 - which I don't want. I want the grid to be empty when users opens the FORM. I only want to reload it when users close FRMNEWCLIENT.

I hope I'm clear. Thanks!

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
BobSki
  • 1,531
  • 2
  • 25
  • 61
  • If you are adding to the underlying datasource there is no refreshing needed - it will automatically be there. – Ňɏssa Pøngjǣrdenlarp Nov 22 '16 at 21:21
  • @plutonix I'm saving it to sql server - I use a dataset to load my grid - but I don't know if it's bound still - siince it doesn't show the new record. Once I search again for all records, it shows me the new one that was added. It works in other cases where I have tabs but iin each case i have to call my function that LOADS the grid in order to see the new data. With the tabs its a different scenario - here I have no clue. If i should set a flag on initial activate of the form and only reload it when I come back from the other form? – BobSki Nov 22 '16 at 21:24
  • Ok, the DataSet contains the table which is the DataSource. If you add new records to that DataTable, they will automatically be visible in anything using that DataTable as long as they match the current filter. Tabs, forms controls - none of that matters – Ňɏssa Pøngjǣrdenlarp Nov 22 '16 at 21:32
  • It depends on what the underlying datasource is. You most likely need to refresh it somehow as most don't look for updates from SQL when a table is modified. – djv Nov 22 '16 at 22:25
  • Thats true if AddNewCust, ViewCust, FindCust, EditCust operations each use their own data objects and write to the DB. As customer operations, they could share the same Customer data objects and actions by each would be available to others as they happen. – Ňɏssa Pøngjǣrdenlarp Nov 22 '16 at 22:35
  • @verdolino yes i basically need to grab data again from sql server and reload it – BobSki Nov 22 '16 at 22:41
  • @plutonix save and edit is directly to server – BobSki Nov 22 '16 at 22:42
  • 1
    @Bobski I'm not posting an answer because I've never done this, but you can look into [CLR Triggers](https://msdn.microsoft.com/en-us/library/938d9dz2(v=vs.100).aspx) which can be used to alert your program when a table is updated. This might be overkill since you are updating from the same application. But it might be useful. – djv Nov 23 '16 at 04:16
  • 1
    Also http://stackoverflow.com/questions/3723551/how-can-i-notify-my-program-when-the-database-has-been-updated – djv Nov 23 '16 at 04:28
  • @verdoline that question might just be what im looking for ill check it out tomorrow at work. Appreciate it – BobSki Nov 23 '16 at 04:38

1 Answers1

2

It sounds like you are looking for a way for one form to know that another has made db changes. Given a form to add or edit a row, you can invoke a method whenever another form adds/changes or deletes a row:

Public Sub SampleDSChanged()
    daSample.Fill(dtSample)      ' refresh
    dgv1.FirstDisplayedScrollingRowIndex = dgv1.RowCount - 1
End Sub

On the form to add/edit a record:

dbcon.Open()
Dim rows = cmd.ExecuteNonQuery()
If rows > 0 Then
    frmMain.SampleDSChanged()
End If

The Edit/New form, INSERTS or UPDATES records as needed using its own SQL statements, acting directly on the db. Afterwards, the method just acts as a notifier. Result:

enter image description here

The 'baby form' is able to add or edit records, in either case it 'tells' the main form that it has done so. The DataGridView is just showing that it gets the notice, it may not actually exist.


Even if you are writing old school form-centric apps, you can leverage the capabilities of the NET provider. Namely, different actors (forms) can make changes to a single datasource so any changes are immediately visible to all concerned parties. You just need to make some of the data objects available.

Even using the same form for ADD and EDIT and an UPSERT method, there is about five times more code involved in having the Edit/Add form act independently than working with a common datasource.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • Thank you for this detailed example. – BobSki Nov 23 '16 at 20:33
  • I completely agree - the way it's been done here is pretty old-school. but i wish I could see a complete example of say ADD/EDIT all working with the same datasource (from what you've been telling me over the last month or so sounds like it would be pretty much something that could be utilized in this app). Also sometimes what we are doing is not simply add edit update because we record our changes in a special table on Edit, we record our DELETES and what not. I've just started programming with vb.net -for the most part i've been doing all vb6 – BobSki Nov 23 '16 at 21:13
  • 1
    I have given you [this link](http://stackoverflow.com/a/33702351) before. Once you have a configured DA, you dont need any other SQL or db code for something like table maintenance; and you can do *real* refreshes - the DA picks up row changes itself. Searching just becomes a matter of applying a row filter. That doesnt rule out piggybacking audit/logging. BTW old school referred to procedural programming vs more OO. Rather than forms with 100s of lines of code duplicating that in other forms, a Customer (or Widget or whatever) class is in charge – Ňɏssa Pøngjǣrdenlarp Nov 23 '16 at 21:27
  • 1
    The code for *that* edit/add form doesnt use a DA - I went behind its back to add/change rows like you seem to be doing on order to prove the concept. To reduce the amount of tedious/redundant code, I did use an UPSERT so the same method could do both. – Ňɏssa Pøngjǣrdenlarp Nov 23 '16 at 21:32