Update Data Grid from Modal/Dialog form

Hi guys,

I have a Main Form with Panel>Inside the Panel is another form>Inside that form is another form. So basically three layers altogether. In the final layer I have a readonly bound data grid. I have written code that opens an edit form for when the user double clicks on the row.

So far the edit form opens the clicked item and saves the changes which is great:)However I would like to refresh the datagrid once it has been saved and see the changes but it only refreshes if I exit the form completely and go back inside???

Really stumped as to what to do, tried changing the datagrid to a public data grid and nothing works? When I open the 3rd layered form alone and perform the task it works without any problems.

I assume it is because of my layering??Your help is much appriciated as always:) Code for the save changes form
    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Validate()

        Me.TblPeopleBindingSource.EndEdit()

        TblPeopleTableAdapter.Fill(TestDBDataSet.tblPeople)
        frmTest2.TblPeopleDataGridView.Invalidate()
        frmTest2.TblPeopleDataGridView.Refresh()

        If Not Me.TblPeopleTableAdapter.Update(Me.TestDBDataSet.tblPeople) > 0 _
        Then
            MsgBox("No records were updated.")
        End If

    End Sub

Open in new window


Code to open edit form from datagrid double click is
        frmEditPerson.Show()
        frmEditPerson.TblPeopleBindingSource.Position = Me.TblPeopleBindingSource.Position

Open in new window

databarracksAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
You have two options

1)Change

frmEditPerson.Show()

to

If frmEditPerson.ShowDialog=DialogResult.Ok Then
    'refresh data
End if

you would need to set the
Me.DialogResult = DialogResult.Ok

in the edit form if changes are made

2) Use Show but subscribe to the FormClosing or FormClosed event of the edit form and refresh data in the event handle

AddHandler frmEditPerson.FormClosing, Addressof yourhandler
0
HomerTNachoCheeseCommented:
I am wondering if you are referencing the data grid incorrectly.  If you are making changes in one of the parent forms and want to refresh the subform, you need to reference it differently.  I am not sure of the exact syntax, but in MS Access forms, it would be like this:
Me!Subform1.Form!ControlName or Me!Subform1.Form!Subform2.Form!ControlName.

I don't program much in .net, but perhaps this might lead you in the right direction.
0
databarracksAuthor Commented:
Hi guys,

To HomerTNachoCheese
In MS Access I have no problem refrencing deeply embedded subforms but in .net I seem to get stuck, as I don't think it is the same in .net

To  Codecruiser
I am going to try your second option as the first doen't make sense to me to be honest. What confuses me is that my approach works when the form with the datagrid is not within the other parent forms etc?
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

databarracksAuthor Commented:
I tried adding
    Private Sub frmEditPerson_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

        TblPeopleTableAdapter.Fill(TestDBDataSet.tblPeople)
        frmTest2.TblPeopleDataGridView.Invalidate()
        frmTest2.TblPeopleDataGridView.Refresh()


    End Sub

Open in new window


To my edit form and nothing. It saves the changes but I have to open and close the main form to see them in the datagrid. Is it not just a reference issue perhaps as HomerTNachoCheese and I are pointing to? This could be cause by being too familiar with Access perhaps?
0
databarracksAuthor Commented:
FYI guys I added my last code as a button on the form with the data grid and it refreshes perfectly. It just seems to have a problem when trying to be refreshed from another form??
0
databarracksAuthor Commented:
Any luck guys?
0
nepaluzCommented:
How do you update the data from your last modal? Is it to the underlying database or directly to the adapter?
0
databarracksAuthor Commented:
Hi there, it was to the adapter.
0
databarracksAuthor Commented:
Hi guys, I think I will close this question now. Thanks for your help despite the fact that we didn't get anywhere but do appreciate your help as always.
0
mbizupCommented:
databarracks,

Before giving up on this, take a look at the question I posted here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Q_27654096.html

I have spent way too much time tearing my hair out trying to figure out how to trigger an action on a main form upon closing a popup form.  The accepted solution by by: COBOLdinosaur shows how to call a javascript function on a parent form using javascript in the popup form (although I wound up using "window.opener" instead of "parent").

So that part works... what might apply in your case is coding the javascript function on the parent form to click an asp button.  If you can use vb.net or C# (or whatever code behind your asp button) to update your datagrid, then the code that I posted in my last comment might do the trick for you.

In summary, that code is:

-  Javascript in the popup to call a javascript function on the main page
-  Javascript function on the main page to click an asp button
- Code behind the button (vb.net or c#) to do 'something else' (such as possibly updating your datagrid)
0
databarracksAuthor Commented:
Hi there,

Thanks for the encouragement but I am not too familiar with the parent function in .net as I am recent convert coming from MS Access which I obviously new how to reference a form within another form etc.

My code
        frmTest2.TblPeopleTableAdapter.Fill(frmTest2.TestDBDataSet.tblPeople)
        frmTest2.TblPeopleDataGridView.Invalidate()
        frmTest2.TblPeopleDataGridView.Refresh()

Open in new window


Is ok when frmTest2 is not buried within frmNewHome(parent)-->frmMain(child)--->frmTest2??
0
mbizupCommented:
<<but it only refreshes if I exit the form completely and go back inside???>>

What is the code you are using to refresh or populate your datagrid?  Is it located in the main form's page load event?
0
databarracksAuthor Commented:
My datagrid is a bound grid via a Table Adapter
0
databarracksAuthor Commented:
    Private Sub frmTest2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'TestDBDataSet.tblPeople' table. You can move, or remove it, as needed.
        Me.TblPeopleTableAdapter.Fill(Me.TestDBDataSet.tblPeople)

        '------Auto Resize--------------'

        TblPeopleDataGridView.AutoResizeColumns()
        TblPeopleDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells



    End Sub

Open in new window

0
databarracksAuthor Commented:
What I don't understand is that all my code works fine when I open the form by itself (not when it is within the other forms etc.) Which is why I keep persevering with the fact that it is a matter of referencing frmTest2 correctly??

But I am not an expert:)
0
mbizupCommented:
I'm fairly green to the ,Net world myself  (similarly with a strong Access background) - so I'm not an Expert by any means, but am just trying to share what has worked for me.

Regarding referencing controls, etc...

Yes - if you just have controls placed on a form, the references are fairly straight-forward.  If they are nested in different layers, the references become a little more complex - and do not necessarily follow the same structure you would use in Access (you can get a feel for that by opening a webpage and looking at the sourcecode).

Anyhow if you are up for it, try a different approach -

1.   Add this javascript to the HTML Markup of your popup form, between the <head></head> tags:
<script language="Javascript" type="text/javascript">


    function callParentFunction()
    {
        window.opener.clickUpdateDG();
        self.close();
    }
</script>

Open in new window


2.  Also in your popup form, add the following to the markup (you can addit towards the bottom of your Form section, so that the test button shows up at the bottom of your popup:

<button id="btnTest" onclick="callParentFunction();"></button>

Open in new window


That's it for the popup form... I'll follow up with parent form code in the next comment
0
mbizupCommented:
3.  In the markup of your main form, add the following - between the <head> tags (or elsewhere, depending on your form layout):
 
   <script language="Javascript" type="text/javascript">
        // This is in the main form - 
        function clickUpdateDG() {
            alert("Hello");
            // This clicks an asp buutton --
            document.getElementById('btnUpdateDG').click();
        }
</script>

Open in new window


4.  Add the following for a command button on the main page (you can hide it later):
            <asp:Button ID="btnUpdateDG" runat="server" Text="Button" />

Open in new window


5.  In the form's design view, double-click that new button and add the following to it's click event:

 
   Protected Sub btnUpdateDG_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
           UpdateDG()        
    End Sub

Open in new window


6.  Add the following sub to your main form's code-behind:

Sub UpdateDG()
        'TODO: This line of code loads data into the 'TestDBDataSet.tblPeople' table. You can move, or remove it, as needed.
        Me.TblPeopleTableAdapter.Fill(Me.TestDBDataSet.tblPeople)

        '------Auto Resize--------------'

        TblPeopleDataGridView.AutoResizeColumns()
        TblPeopleDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

End Sub

Open in new window



See if that helps... if not, I am out of ideas...
0
databarracksAuthor Commented:
Ok Let me give it a go and will let you know shortly. Thank you again for your help
0
CodeCruiserCommented:
@ mbizup

You do realize we are dealing with Winforms not ASP.NET?

@ databarracks
Option two was for the main form to subscribe to the close event of edit form and refresh itself when the edit form closes.

Mainform:

Dim frm As New EditForm
AddHandler frm.FormClosing, AddressOf EditForm_Closing
frm.Show


you would need the EditForm_Closing handler on the main form.

Private Sub EditForm_Closing(...)

Option 1 can work as well. If you need further clarification on that then let me know.
0
databarracksAuthor Commented:
Hi CodeCruiser

Thank you so much for clarifying that I was using Winforms:) Trying this now
0
databarracksAuthor Commented:
Where do I put the
    Dim frm As New frmAddNewPerson
    Private Sub frmAddNewPerson_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        frm.Show()

Open in new window


In the main form??
0
mbizupCommented:
Ooops - sorry about that :-/
0
CodeCruiserCommented:
That code is an example. You are using similar code to show the frmEditPerson form right? Just modify that code and put the AddHandler line before the Show line.
0
databarracksAuthor Commented:
Hi Guys,

Almost there, however I am getting an error on my main form code.
        Dim frm As New frmAddNewPerson
        AddHandler frm.FormClosing, AddressOf frmAddNewPerson_FormClosing
        frm.Show()

Open in new window


The error is 'frmAddNewPerson_FormClosing' is not declared. It may be inaccessible due to its protection level. I ithnk I have finally caught on to the logic of your method and feel it ias about to work :). Could you please advise on the error?
0
CodeCruiserCommented:
Put following in your main form

Private Sub frmAddNewPerson_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
   'Put code here to reload data
End Sub
0
databarracksAuthor Commented:
That will go prior to me opening the edit form correct?
0
databarracksAuthor Commented:
Getting error statement cannot appear within a method body.End of method assumed. Current code
    Private Sub AddNewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNewToolStripMenuItem.Click

    Private Sub frmAddContact_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
        TblPeopleTableAdapter.Fill(TestDBDataSet.tblPeople)
        TblPeopleDataGridView.Invalidate()
        TblPeopleDataGridView.Refresh()
        frmAddContact.Show()
    End Sub

Open in new window

0
databarracksAuthor Commented:
Hi again I pasted the wrong form name it was meant to be
    Private Sub AddNewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNewToolStripMenuItem.Click

    Private Sub frmAddNewPerson_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
        TblPeopleTableAdapter.Fill(TestDBDataSet.tblPeople)
        TblPeopleDataGridView.Invalidate()
        TblPeopleDataGridView.Refresh()
        frmAddNewPerson.Show()
    End Sub

Open in new window

0
databarracksAuthor Commented:
Nevertheless I still don't know what to do?I am an amateur clearly but appreciate all the help
0
CodeCruiserCommented:
Try following

Private Sub AddNewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNewToolStripMenuItem.Click
 Dim frm As New frmAddNewPerson
        AddHandler frm.FormClosing, AddressOf frmAddNewPerson_FormClosing
        frm.Show()
End Sub

    Private Sub frmAddNewPerson_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
        TestDBDataSet.tblPeople.Clear()
        TblPeopleTableAdapter.Fill(TestDBDataSet.tblPeople)
        TblPeopleDataGridView.Invalidate()
        TblPeopleDataGridView.Refresh()
    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
databarracksAuthor Commented:
I cannot believe it:) It worked hooray!!. Thank you all for your help guys and as usual you have come through as always.

CodeCruiser I am indebted to you and thank you for all your help. Absolutely brilliant, nothing else to say other than genius:)
0
databarracksAuthor Commented:
Just wanted to say that all the experts were involved as usual and shone through with their brilliance again. Special thanks to CodeCruiser who was especially helpful and very patient. Outstanding workd from him and the others
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.