Question How do you refresh a datagrid from a different form?

emaduddeen

Well-known member
Joined
May 5, 2010
Messages
171
Location
Lowell, MA & Occasionally Indonesia
Programming Experience
Beginner
Hi Everyone,

I have an app that opens up 3 forms. 2 forms have a datagrid the other form has book lending details. The datagrid forms are for books in the library and books that have been borrowed.

Both datagrid forms will be open as shown in the attachment, how do I refresh the "Borrowed Books" datagrid form from inside the form that has the "Book in Library" datagrid.

So far I've been able to update the "Books in Library" datagrid because control comes back to that form after the details form closes but since the "Borrowed Books" form is still opened up, we would like to refresh the datagrid on that form.

data grid refresh.jpgThanks.

Truly,
Emad
 
What does "refresh the datagrid" actually mean for a start?

Regardless, you basically have two options: the easy way and the proper way.

1. Assuming that each form is the default instance of its type, you can simply refer to a form by its class name, then access any public members as you would any other class. That means that each form can directly access the controls on any other form. That's exactly why default instances were created. For more information, follow the Blog link in my signature and read the post about default instances.

2. You can generally think of two forms as having a parent/child relationship, where the parent was open first and it opens the child. In such cases, either the parent wants to affect the child or the child wants to affect the parent. In the first instance, the parent created the child so it simply uses the reference it already has to set a property or call a method of the child. Anything that actually changes the child is then done by the child in that property or method. If the child wants to affect the parent, the child should raise an event that the parent can handle. Anything that actually changes the parent is then done by the parent in that event handler. This is the proper way to write code. Each object is responsible for itself, with the only interface being properties, methods and events.
 
Hi jmcilhinney,

I wish to code it the proper way.

Could you show me some code samples on how to do it?

When the app starts, the MDI MainForm is displayed. The user opens the FormBooksInLibrary form and then the user opens the FormBooksBorrowed form using buttons on the MainForm.

The class names for all these forms are the same as the form names.

I'm assuming the code to control the refreshing will be on the MainForm.

Perhaps you have an app in the code bank that shows this technique ?

Thanks.

Truly,
Emad
 
Back
Top