Bobski Bobski - 10 days ago 4
Vb.net Question

child forms - performing action on Activated Event

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!

Answer

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.

Comments