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
Code to open edit form from datagrid double click is
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
Code to open edit form from datagrid double click is
frmEditPerson.Show()
frmEditPerson.TblPeopleBindingSource.Position = Me.TblPeopleBindingSource.Position
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!ControlNa me or Me!Subform1.Form!Subform2. Form!Contr olName.
I don't program much in .net, but perhaps this might lead you in the right direction.
Me!Subform1.Form!ControlNa
I don't program much in .net, but perhaps this might lead you in the right direction.
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?
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?
ASKER
I tried adding
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?
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
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?
ASKER
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??
ASKER
Any luck guys?
How do you update the data from your last modal? Is it to the underlying database or directly to the adapter?
ASKER
Hi there, it was to the adapter.
ASKER
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)
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)
ASKER
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
Is ok when frmTest2 is not buried within frmNewHome(parent)-->frmMa in(child)- -->frmTest 2??
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()
Is ok when frmTest2 is not buried within frmNewHome(parent)-->frmMa
<<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?
What is the code you are using to refresh or populate your datagrid? Is it located in the main form's page load event?
ASKER
My datagrid is a bound grid via a Table Adapter
ASKER
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
ASKER
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:)
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:
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:
That's it for the popup form... I'll follow up with parent form code in the next comment
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>
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>
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):
4. Add the following for a command button on the main page (you can hide it later):
5. In the form's design view, double-click that new button and add the following to it's click event:
6. Add the following sub to your main form's code-behind:
See if that helps... if not, I am out of ideas...
<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>
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" />
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
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
See if that helps... if not, I am out of ideas...
ASKER
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.
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.
ASKER
Hi CodeCruiser
Thank you so much for clarifying that I was using Winforms:) Trying this now
Thank you so much for clarifying that I was using Winforms:) Trying this now
ASKER
Where do I put the
In the main form??
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()
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.
ASKER
Hi Guys,
Almost there, however I am getting an error on my main form code.
The error is 'frmAddNewPerson_FormClosi ng' 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?
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()
The error is 'frmAddNewPerson_FormClosi
Put following in your main form
Private Sub frmAddNewPerson_FormClosin g(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormC losingEven tArgs)
'Put code here to reload data
End Sub
Private Sub frmAddNewPerson_FormClosin
'Put code here to reload data
End Sub
ASKER
That will go prior to me opening the edit form correct?
ASKER
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
ASKER
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
ASKER
Nevertheless I still don't know what to do?I am an amateur clearly but appreciate all the help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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:)
CodeCruiser I am indebted to you and thank you for all your help. Absolutely brilliant, nothing else to say other than genius:)
ASKER
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
1)Change
frmEditPerson.Show()
to
If frmEditPerson.ShowDialog=D
'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,