You have like five problems at once. Writing a proper answer would require a book (which I would suggest you to go read anyway), so this answer is a summary you can use to learn more.
First, you need to elaborate on "when there is an update happen[ing] in other forms". You need to detect this change. How to do that, depends on how that form works. Hopefully it does using data binding and INotifyPropertyChanged
, see Raise an event whenever a property's value changed?.
Then on these "other forms", you subscribe to their model's PropertyChanged
event and propagate that as an event on each form. Be sure to unsubscribe when appropriate as well. In the PropertyChanged
event handler of your form, you raise an event that's specific to that form, like MyModelChanged
.
Now you have a form that can notify interested parties of events, by subscribing to that event.
Something like this:
var yourEditForm = new YourEditForm();
yourEditForm.MyModelChanged += this.YourEditForm_MyModelChanged;
yourEditForm.Show();
Now where you place this code is pretty crucial. When working with multiple forms you want to communicate with each other, you need some kind of "controller" (or give it a name) that knows about all forms and their events that are relevant to your application, and ties it all together.
So in your controller you now have the above code and this event handler:
private void YourEditForm_MyModelChanged(object sender, EventArgs e)
{
}
Now in that event handler, you can let your aptly named Form1
reload its data. You can do so by exposing a public method that does just that:
public void RefreshGrid()
{
cont.etudiant1.Load();
etudiant1DataGridView.DataSource = cont.etudiant1.Local;
}
There's your "refresh". You can call form1.RefreshGrid()
in the event handler shown above.
Note that all of this is pretty much hacked together. Go read a tutorial or two about data binding in WinForms to let this properly be handled, because doing it manually is going to be a pain to maintain.
You can start by reading Data Binding and Windows Forms and Change Notification in Windows Forms Data Binding on MSDN.