14 Replies - 62826 Views - Last Post: 17 September 2007 - 07:52 AM Rate Topic: -----

#1 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Best way to update parent form after child form closes

Post icon  Posted 29 August 2007 - 10:21 PM

Any ideas on the best way to update a parent form when it has a datagrid open, filled with data from an sql 2005 database. A child form is opened, that data is manipulated and then the child form is closed... I need the parent form to then reflect those changes... I can even re-query and it doesn't show the changes, not until I close the application and re-open it. :crazy:

Is This A Good Question/Topic? 0
  • +

Replies To: Best way to update parent form after child form closes

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Best way to update parent form after child form closes

Posted 30 August 2007 - 05:24 AM

webwired,

Are you populating the DataGridView on the parent form in the Load event of the form? Is the grid populated on the parent form based on a parameter or is it loading all the data from the table? If so, in the FormClosing event of the child form call the Load event of the parent form, like so

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
	'Unbind the DataGridView control in the parent form
	 ParentForm.DataGridView1.Items.Clear()
	'Now call the Load event of the parent form
	 ParentForm.Load(Nothing,Nothing)
End Sub



That should do the trick. Let me know how it goes :)

Happy Coding!
Was This Post Helpful? 0
  • +
  • -

#3 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: Best way to update parent form after child form closes

Posted 30 August 2007 - 01:04 PM

The datagrid on the main form in loaded based on a search parameter.

On the main form, I have 3 datagrids layered on top of one another. By default, the customer datagrid is on top, so that a user does a search for a customer, then double clicks on the datagrid of the correct customer that they were looking for. Once the user double clicks their selection, the buttons appear to allow the user to view the corresponding invoices and contacts for that customer. Those forementioned buttons simply make not visible and make visible the other datagrids.

The problem lies in when I call up another form to add a contact or invoice and then that is done, it won't show up on the datagrid, even if you re-query that particular customer... Which of course would suck even if you had to do that, ideally, like you said, if on a sub form close event could trigger an on load event of the main form, making it refresh,... but I don't see how that's possible if it isn't even showing up in the database until after the application is closed and then reopened.

Would you like to see the program?
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Best way to update parent form after child form closes

Posted 30 August 2007 - 01:22 PM

Yes actually I would, because when you insert a record into a table its there immediately, it doesnt wait for the application to close to add it. You may have to unbind then re-bind the DGV, or use the DataGridView1.Refresh() method.
Was This Post Helpful? 0
  • +
  • -

#5 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: Best way to update parent form after child form closes

Posted 30 August 2007 - 01:27 PM

Here's the whole folder, zipped up. Thanks alot for taking a look PsychoCoder, project is due Tuesday and I'm stressing. This is the seventh one of this project, scrapped the first 6 in favor of this less form one.

I tried the InvoicesDataGridView.Refresh() method called by a refresh button, it still didn't show the recently input record, only after I closed and opened the program.

Start the C# class the week after labor day week... LOL... you'll see me in the C# forum then,... Hmmm, wonder if I can use the same final project, just rewrite my code... :D

Attached File(s)

  • Attached File  cis.zip (655.72K)
    Number of downloads: 988

This post has been edited by webwired: 30 August 2007 - 04:48 PM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Best way to update parent form after child form closes

Posted 30 August 2007 - 05:15 PM

webwired,

Heres something I want you to try (I cant test it as I dont have the database here). In your saveButton_Click Event in AddInvoiceForm.vb right below the InsertMyDate call add this

MainForm.InvoicesBindingSource.ResetBindings(False)
MainForm.InvoicesDataGridView.Refresh()



This does 2 things
  • Causes your BindingSource on MainForm to rebind itself
  • Tells the InvoicesDataGridView to refresh itself

If this works the way I think it will (if it don't Ill be shocked, as this is how I would do it), then do that for the other forms that update the DataGridViews on MainForm (except change the name of the stuff to correspond to that particular DataGridView)

Hope this helps :)

Happy Coding!
Was This Post Helpful? 0
  • +
  • -

#7 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: Best way to update parent form after child form closes

Posted 30 August 2007 - 05:25 PM

It sure sounded good, but it didn't work...

Wasn't the database in the zip folder with the rest of it?

Here's what I did, like you said...

		InsertMyData(ConnString, customerId, invoiceDateTime, discountRate, inStoreSale, shipToAddr, shipToCity, shipToState, shipToZip, shippingType, shippingPriority, pmtType, paid, paidDate, specInstr, tax, total)
		MainForm.InvoicesBindingSource.ResetBindings(False)
		MainForm.InvoicesDataGridView.Refresh()


Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Best way to update parent form after child form closes

Posted 30 August 2007 - 05:55 PM

webwired,

As much as I know about VB.Net, and as long as Ive used it, Ive never used those adapters, dataset and such controls you drag onto the form so I'm going to have to do a little research to see how you refresh them (as they don't seem to work the same way as they do when created in code). So give me a little time and Ill see what I can come up with ok :)
Was This Post Helpful? 0
  • +
  • -

#9 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: Best way to update parent form after child form closes

Posted 30 August 2007 - 06:04 PM

Ok, np, I certainly appreciate any and all help...

The only reason that I was forced to use those darn drag and drop datasets was because of the datagrids, didn't know how to populate my own made datagrid. That's where me and my instructor disagree, he likes 'em, I don't. Personally I feel that they are not very, end user friendly.

This post has been edited by webwired: 30 August 2007 - 06:11 PM

Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Best way to update parent form after child form closes

Posted 30 August 2007 - 06:41 PM

In your AddInvoiceForm change the end of your InsertMyData procedure to

InvoiceForm.ShowDialog()
MainForm.InvoicesBindingSource.ResetBindings(False)
MainForm.InvoicesDataGridView.Refresh()
Me.Close()



Putting it where I had it earlier does no good as in this procedure you call Me.Close so putting it after the call to InsertMyData in the button click event means it never gets ran as the form is already closed.

You could also try putting it in the AddInvoiceForm_FormClosing event as well, whatever is in that event executes as the form is closing
Was This Post Helpful? 0
  • +
  • -

#11 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: Best way to update parent form after child form closes

Posted 30 August 2007 - 07:13 PM

well, I tried putting it in the procedure, where you told me,... it didn't work... then I tried putting it in the form closing event, still didn't work...

	Public Sub InsertMyData(ByVal myConnString As String, _
						ByVal invoice_customer_id As String, _
						ByVal invoice_date As DateTime, _
						ByVal invoice_discount_rate As String, _
						ByVal invoice_instore_sale As String, _
						ByVal invoice_shipto_addr As String, _
						ByVal invoice_shipto_city As String, _
						ByVal invoice_shipto_state As String, _
						ByVal invoice_shipto_zip As String, _
						ByVal invoice_shipping_type As String, _
						ByVal invoice_shipping_priority As String, _
						ByVal invoice_pmt_type As String, _
						ByVal invoice_paid As String, _
						ByVal invoice_paid_date As DateTime, _
						ByVal invoice_special_instructions As String, _
						ByVal invoice_tax As String, _
						ByVal invoice_total As String)
		Dim myInsertQuery As String = "INSERT INTO invoices VALUES( '" & invoice_customer_id & "', '" & invoice_date & "', '" & _
				invoice_discount_rate & "', '" & invoice_instore_sale & "', '" & invoice_shipto_addr & "', '" & invoice_shipto_city & "', '" & invoice_shipto_state & "', '" & invoice_shipto_zip & "', '" & invoice_shipping_type & "', '" & _
				invoice_shipping_priority & "', '" & invoice_pmt_type & "', '" & invoice_paid & "', '" & invoice_paid_date & "', '" & invoice_special_instructions & "', '" & invoice_tax & "', '" & invoice_total & "' )"
		Dim myConnection As New SqlConnection(myConnString)
		Dim myCommand As New SqlCommand(myInsertQuery, myConnection)
		Dim retvalue As Integer
		myConnection.Open()
		retvalue = myCommand.ExecuteNonQuery()
		myCommand.CommandText = "SELECT SCOPE_IDENTITY() statement"
		Dim insertedInvoiceId As String = myCommand.ExecuteScalar().ToString
		myConnection.Close()
		My.Computer.FileSystem.WriteAllText("F:\cis\NewInvoiceId.txt", insertedInvoiceId, False)
		MainForm.InvoicesBindingSource.ResetBindings(False)
		MainForm.InvoicesDataGridView.Refresh()
		Me.Close()
	End Sub



	Private Sub AddInvoiceForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
		MainForm.InvoicesBindingSource.ResetBindings(False)
		MainForm.InvoicesDataGridView.Refresh()
	End Sub


Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Best way to update parent form after child form closes

Posted 30 August 2007 - 07:18 PM

Then it has something to do with the way those physical controls work, and its starting to really stump me :crazy:
Was This Post Helpful? 0
  • +
  • -

#13 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: Best way to update parent form after child form closes

Posted 30 August 2007 - 07:31 PM

That can't be good then... :)
Was This Post Helpful? 0
  • +
  • -

#14 Sylan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-September 07

Re: Best way to update parent form after child form closes

Posted 16 September 2007 - 04:34 PM

Hi, just stumbled over this post.
If you haven't solved it yet I may have a solution for you:

I was struggeling with the same thing except with updating comboboxes with updated data.

Try this

MainForm.InvoicesBindingSource.Position = MainForm.InvoicesBindingSource.IndexOf('The datarow you are updating')





Hope this helps...
Was This Post Helpful? 0
  • +
  • -

#15 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: Best way to update parent form after child form closes

Posted 17 September 2007 - 07:52 AM

Actually yeah, I was still stumped on that one, I was having to redo the entire query... big waste of resources... that will help alot, thanks...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1