Link to home
Start Free TrialLog in
Avatar of databarracks
databarracks

asked on

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

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of HomerTNachoCheese
HomerTNachoCheese

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.
Avatar of databarracks

ASKER

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?
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?
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??
Any luck guys?
How do you update the data from your last modal? Is it to the underlying database or directly to the adapter?
Hi there, it was to the adapter.
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.
databarracks,

Before giving up on this, take a look at the question I posted here:
https://www.experts-exchange.com/questions/27654096/Inform-webpage-that-popup-has-closed.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)
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??
<<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?
My datagrid is a bound grid via a Table Adapter
    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

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:)
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
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...
Ok Let me give it a go and will let you know shortly. Thank you again for your help
@ 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.
Hi CodeCruiser

Thank you so much for clarifying that I was using Winforms:) Trying this now
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??
Ooops - sorry about that :-/
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.
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?
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
That will go prior to me opening the edit form correct?
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

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

Nevertheless I still don't know what to do?I am an amateur clearly but appreciate all the help
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:)
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