I have two forms. Form A and Form B.
Form A has a datagrid that is populated from a SQL Database Table query. When a button is pressed Form B is launched that allows you to add a record to the database table. I need for the datagrid to be refreshed and show the newly added record when I close form B and go back to the parent.

I thought this would be easy, but it has been giving me fits.

I'm using a SQL Query to populate the DataGridView control, but I need to Requery the table again to repopulate it I think.

Can anyone give me any suggestions on how to do this?

I was looking for a way to re-fire the form load event but I can't figure out how.

Any help would be appreciated,

Thanks in advance,

Dani AI

Generated

Good call by — don’t try to re-fire the Form.Load event. Move the database/fill logic into a single public method on FormA and invoke that from wherever you need a refresh. This keeps initialization distinct from runtime updates and is what allowed to get it working quickly.

Example — parent method + direct call (simple and explicit):

' FormA
Public Sub RefreshGrid()
    Dim dt As New DataTable()
    Using cn As New SqlConnection(connectionString)
        Using da As New SqlDataAdapter("SELECT * FROM MyTable", cn)
            da.Fill(dt)
        End Using
    End Using
    BindingSource1.DataSource = dt
    DataGridView1.DataSource = BindingSource1
End Sub

' When opening FormB modally from FormA:
Using f As New FormB()
    f.ShowDialog(Me)
    If f.DialogResult = DialogResult.OK Then RefreshGrid()
End Using

Example — event-driven (decoupled, recommended when FormB can be used by different parents):

' FormB
Public Event RecordAdded As EventHandler

Private Sub SaveRecord()
    ' insert into DB
    RaiseEvent RecordAdded(Me, EventArgs.Empty)
    Me.DialogResult = DialogResult.OK
    Me.Close()
End Sub

' FormA (subscribe before showing)
Dim f As New FormB()
AddHandler f.RecordAdded, AddressOf RefreshGrid
f.ShowDialog(Me)

Troubleshooting notes: make RefreshGrid Public; if you call parent from child check If Me.Owner IsNot Nothing before casting; if using a BindingSource call BindingSource1.ResetBindings(False) or reassign the DataSource after refill; avoid cross-thread UI updates (use Invoke if the DB work runs on a background thread). For small inserts you can append a DataRow to the current DataTable instead of re-querying the entire table.

Recommended Answers

All 2 Replies

You shouldnt call the Load event at all to be honest. Calling the load event at runtime will have sometimes very bad side effects.
Put your code for filling the datagrid in a seperate function and call this function in the load event of FormA and from FormB u call it FormA.myFunction

Thank you so much for this post. After creating a custom function to populate the datagridview control it was very easy for me to fire it from outside the form.

Thanks So Much!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.