Friday, March 23, 2012

Refresh a windows form without closing it

Hi all

I have a couple of windows forms which share tables or parts of tables.

When I edit a tables data on one form (form2), where it ,s data is linked to another form ( form1), when I go back to form1 I find that the fields have not been updated until I close the form and reopen it.

Is there a way to refresh the field on form1 by using a button in the menu bar and what would the code behind look like. I use vb .

Thanks

Rob

You can create a button and then in the click event use:

me.refresh which will refresh the whole form, or you can do controlname.refresh which will refresh just the specific control.

|||

Hi

Thanks for your help so far. I have tried the "me. refresh ()" but nothing happens.

I have also added a few things ( as below) to the code to try and get my dataset to refresh , but still without succsess . Can you suggest any other code snippit which I can add to the me.refresh() which would refresh the data in the client dataset.

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click

Me.Refresh = ProjectManagmentDataSet1.Client()

End Sub

Thanks

Rob

|||You need to re-fill the dataset. If you are using a table adapter, it would be like the following:

Code Snippet

Me.MyTableAdapter.Fill(Me.MyDataSet.MyTable)

If you look in the Form_Load event, this is usually where the code is located that will load your data. You can simply copy those steps into the ToolStipButton1_Click event. Once the dataset has been re-filled, use Me.Refresh() to refresh the form. If that doesn't make sense, could you post the code that you are using to load the data into your form?

|||

Thanks for your patience so far

Public Class Form2

Private Sub ClientBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClientBindingNavigatorSaveItem.Click

Me.Validate()

Me.ClientBindingSource.EndEdit()

Me.ClientTableAdapter.Update(Me.ProjectManagmentDataSet1.Client)

Me.SHIPSBindingSource.EndEdit()

Me.SHIPSTableAdapter.Update(Me.ProjectManagmentDataSet1.SHIPS)

End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'TODO: This line of code loads data into the 'ProjectManagmentDataSet1.SHIPS' table. You can move, or remove it, as needed.

Me.SHIPSTableAdapter.Fill(Me.ProjectManagmentDataSet1.SHIPS)

'TODO: This line of code loads data into the 'ProjectManagmentDataSet1.Client' table. You can move, or remove it, as needed.

Me.ClientTableAdapter.Fill(Me.ProjectManagmentDataSet1.Client)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Client.Show()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Form1.Show()

End Sub

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click

Me.Refresh()

End Sub

End Class

|||

Can you show me the code for Form1 as well? I think that is where we need to do the updating. If I understand your question correctly, you edit in Form2 and need it to update into Form1 without having to close and re-open Form1.

One other item that is not really a huge thing but you may want to change the ClientBindingNavigatorSaveItem_Click to the following:

Private Sub ClientBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClientBindingNavigatorSaveItem.Click

Try

Me.Validate()

Me.ClientBindingSource.EndEdit()

Me.ClientTableAdapter.Update(Me.ProjectManagmentDataSet1.Client)

Me.SHIPSBindingSource.EndEdit()

Me.SHIPSTableAdapter.Update(Me.ProjectManagmentDataSet1.SHIPS)

Catch ex as Exception

MsgBox("Update Failed: " & ex.ToString)

End Try

End Sub

This will pop up a message box with an explanation as to why the database was not able to be updated if there is a problem updating the data.

|||

This is as far as I have gotten. I edit in form1and hope to refresh form2 without closing and reopening.

I have no problem with updating the data , just the long rout around of closing and reopening forms.

Regards

Rob

|||

OK, the code in form2 should be like the following:

Code Snippet

Public Class Form2

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Call loadData()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Client.Show()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Form1.Show()

End Sub

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click

Call loadData()

Me.Refresh()

End Sub

Private Sub loadData()

Me.SHIPSTableAdapter.Fill(Me.ProjectManagmentDataSet1.SHIPS)

Me.ClientTableAdapter.Fill(Me.ProjectManagmentDataSet1.Client)

End Sub

End Class

You shouldn't need the "save" part of the code in form2, just in form1. All you need to do is to fill the adapters again and then refresh the form.

|||Thanks ,all sorted out.

No comments:

Post a Comment