[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Update Data Grid from Modal/Dialog form

Posted on 2012-03-29
32
Medium Priority
?
595 Views
Last Modified: 2012-04-10
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

0
Comment
Question by:databarracks
  • 20
  • 5
  • 5
  • +2
32 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37782971
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
 
LVL 6

Expert Comment

by:HomerTNachoCheese
ID: 37782989
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
 

Author Comment

by:databarracks
ID: 37783020
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

Author Comment

by:databarracks
ID: 37783042
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
 

Author Comment

by:databarracks
ID: 37783093
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
 

Author Comment

by:databarracks
ID: 37783370
Any luck guys?
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37791747
How do you update the data from your last modal? Is it to the underlying database or directly to the adapter?
0
 

Author Comment

by:databarracks
ID: 37794788
Hi there, it was to the adapter.
0
 

Author Comment

by:databarracks
ID: 37800102
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
 
LVL 61

Expert Comment

by:mbizup
ID: 37800340
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
 

Author Comment

by:databarracks
ID: 37800395
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
 
LVL 61

Expert Comment

by:mbizup
ID: 37800497
<<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
 

Author Comment

by:databarracks
ID: 37800517
My datagrid is a bound grid via a Table Adapter
0
 

Author Comment

by:databarracks
ID: 37800518
    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
 

Author Comment

by:databarracks
ID: 37800544
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
 
LVL 61

Expert Comment

by:mbizup
ID: 37800639
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
 
LVL 61

Expert Comment

by:mbizup
ID: 37800702
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
 

Author Comment

by:databarracks
ID: 37800800
Ok Let me give it a go and will let you know shortly. Thank you again for your help
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37802091
@ 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
 

Author Comment

by:databarracks
ID: 37802261
Hi CodeCruiser

Thank you so much for clarifying that I was using Winforms:) Trying this now
0
 

Author Comment

by:databarracks
ID: 37802294
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
 
LVL 61

Expert Comment

by:mbizup
ID: 37802833
Ooops - sorry about that :-/
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37803179
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
 

Author Comment

by:databarracks
ID: 37804864
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37810520
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
 

Author Comment

by:databarracks
ID: 37812054
That will go prior to me opening the edit form correct?
0
 

Author Comment

by:databarracks
ID: 37812136
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
 

Author Comment

by:databarracks
ID: 37812167
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
 

Author Comment

by:databarracks
ID: 37812200
Nevertheless I still don't know what to do?I am an amateur clearly but appreciate all the help
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1440 total points
ID: 37814661
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
 

Author Comment

by:databarracks
ID: 37826552
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
 

Author Closing Comment

by:databarracks
ID: 37826558
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month17 days, 14 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question