Question How do I iterate through the forms in my program to deal with DataGridView controls in each form?

Robert Homes

Member
Joined
Nov 16, 2023
Messages
11
Programming Experience
5-10
My program has several forms that contain a DataGridView (I call each one "dg"). I want to be able to issue a change to the DG's without having to go into each one and make the change. How do I write code to iterate through the forms in the program and deal with the DG control in each one automatically?

There is something called "Owned Forms" I could use. But how to designate the DG controls in each one?
 
I just figured it out myself. You interate through the forms look in each one for a control named "dg"

For each frm in OwnedForms
for each Item in frm.controls
if item.name = "dg" then ... etc.

Thanks for the opportunity to figure this out myself...
 
I'm not sure that OwnedForms is what you want. That is a collection of forms owned by the current form. Is every form containing a grid really an owned form of the current form? I guess it could be the case but owned forms are usually used for something like a Find & Replace dialogue, where it's something like a modal dialogue but doesn't prevent access to the caller.
 
There is OpenForms that returns all currently open form instances:
VB.NET:
For Each frm As Form In My.Application.OpenForms
 
Back
Top