-2

I'm using entity framework 5 on my winform application. I have a datagridView on my form which contains data from my database:

public partial class Form1 : Form
{
    etudiantEntities cont = new etudiantEntities();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        cont.etudiant1.Load();

        etudiant1DataGridView.DataSource = cont.etudiant1.Local;
    }

Right now, every thing is perfect. Now, i want to reload data when there is an update happened in other forms. I want to reload it periodically. Is there a way to do that with entity framework? Thank you!

Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
  • Have you heard of "events"? They're a fundamental building block of WinForms and used to propagate things that happen in your application. – CodeCaster Apr 06 '16 at 10:00
  • the thing which i don't know is how to reload data updated outsid of the opened form. – Ahmed Fantar Apr 06 '16 at 10:04
  • 1
    it's your opinion! – Ahmed Fantar Apr 06 '16 at 10:11
  • 1
    No it's not, it's the basic premise of this site: search and research before you ask. Googling "C# winforms share data between forms" or "C# winforms subscribe to event on other form" will yield thousands of pages that you can use to enlighten yourself. – CodeCaster Apr 06 '16 at 10:14
  • i 'm not asking for share data between forms, i want to have the newest data on the opened form – Ahmed Fantar Apr 06 '16 at 10:18

1 Answers1

1

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.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272