Rad
Rad

Reputation: 4931

Updating data from other form


I have tow form,ListFrom, and DetailForm
In the ListForm I have a devexpress grid and some button(add, delete, edit)
In the DetailForm I have some textboxes and some button(save,delete,next,previous)
well I have to senario
1 - I open the ListForm and I click on a product to modify it a got the DetailForm opened, I make some modification and I save,then i should have my grid in the ListForm refreshed with the new value.for this I have this code

In the ListFrom

FrmProduit frm = new FrmProduit(monProduit.Id) { MdiParent = this.MdiParent};
frm.updateDataInGridView += new System.Action(refereshGridView);
frm.Show();

in the detailform

 if (updateDataInGridView != null)
      updateDataInGridView();

well in this scenario everything is OK

second scenario If I open the detailFrom,and after that I open the listForm, I make some change in the detailFrom and I click save updateDataInGridView in this case is null and then the grid is not refreshed anyone have suggestion?

Upvotes: 2

Views: 88

Answers (2)

gzaxx
gzaxx

Reputation: 17600

I would create a shared BindingSource that both forms would use to show data. If any item is changed in BindingSource it takes care to notify all controls bind to it and so it would refresh grid automatically.

Second approach is to make refereshGridView method public and in DetailForm on save click do this:

var lists = Application.OpenForms.OfType<Form>().Where(x => x.GetType() == typeof(ListFrom));

foreach (var listform in lists)
{
    listform.refereshGridView();
}

I did not use FirstOrDefault as maybe there is more than one listform opened.

EDIT about Binding Source

Here is quite good tutorial so please take a look.

Below is a fast-written far from best example of stretch I did:

internal static class DataSources
{
     private static BindingSource bs;

     public static BindingSource CerateDataSource(List<object> yourObjects)
     {
          bs = new BindingSource();
          bs.DataSource = yourObjects;
     }

     public static BindingSource GetDataSource()
     {
          return bs;
     }

     public static void Reset()
     {
          bs.ResetBindings(false);
     }
}

and then in your listview

dataGridView1.DataSource = DataSources.GetData();

and in detailsview where you are editing one of the objects from BindingSource on save you would have to call: DataSources.Reset();. This is just a markup, but hopefully you get the idea :).

Upvotes: 1

apomene
apomene

Reputation: 14389

You must always be sure you are referring to the current instance of detailform, thus declare on your listForm

detailform obj = (detailform)Application.OpenForms["detailform"];

And every time you call detailform from listForm do it by obj e.g:

obj.Show()

Upvotes: 0

Related Questions