1

I have this situation I want to synchronize informations in my dataGridView when I insert it on my add form like you can see on this picture.enter image description here

In my Insert form on insert button I call Add form to pop up like this

  private void button1_Click(object sender, EventArgs e)
    {
        if (addForm==null)
        {
            addForm = new AddForm();
        }
        addForm.MdiParent = this.ParentForm;
        addForm.FormClosed += AddForm_FormClosed;
        addForm.Show();

    }

    private void AddForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        addForm = null;
    }

On Add form in Accept click I insert informations and call fillDataGrid() method from Insert form to do data sync but nothing is shown data is shown just when I close Insert form and call it again does someone has susggestion how can I do this this is the first time I work with MdiContainer ?

private void buttonAccept_Click(object sender, EventArgs e)
    {
        if (validation())
        {
            Proizvod product = new Proizvod();
            product.NazivProizvoda = textBoxName.Text;
            product.Opis = textBoxDescription.Text;
            product.SerijskiBroj = textBoxNumber.Text;
            product.ZemljaPorijekla = textBoxCountry.Text;

            if (pDal.insertProduct(product)==0)
            {
                MessageBox.Show("Informations are successfully inserted","Message");
                InsertForm inForm = new InsertForm();
                inForm.fillDataGrid();
            }
        }
    }

My fillDataGrid() method and Load event of InsertForm:

 public void fillDataGrid()
    {
        dataGridViewProducts.DataSource = null;
        dataGridViewProducts.AutoGenerateColumns = false;
        dataGridViewProducts.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        dataGridViewProducts.ColumnCount = 3;

        dataGridViewProducts.Columns[0].Name = "Product name";
        dataGridViewProducts.Columns[0].DataPropertyName = "NazivProizvoda";
        dataGridViewProducts.Columns[1].Name = "Country";
        dataGridViewProducts.Columns[1].DataPropertyName = "ZemljaPorijekla";
        dataGridViewProducts.Columns[2].Name = "Product number";
        dataGridViewProducts.Columns[2].DataPropertyName = "SerijskiBroj";
        dataGridViewProducts.DataSource = pDal.getAllProducts();
    }

    private void InsertForm_Load(object sender, EventArgs e)
    {
        fillDataGrid();
    }

    private void InsertForm_Shown(object sender, EventArgs e)
    {
        dataGridViewProducts.CurrentCell = null;
        dataGridViewProducts.ClearSelection();
    }
jhony3
  • 272
  • 1
  • 2
  • 13
  • The model (database stuff) can provide `ProductAdded` event, when something calls model method `AddProduct(product)` then add product and rise that event. – Sinatr Aug 05 '16 at 13:09
  • Sorry @Sinatr I really have no idea how to do that you mean for me to write my delegate and event – jhony3 Aug 05 '16 at 13:22
  • 1
    Currently in `buttonAccept_Click` code, you have created a new instance of the list form and called its `FillGrid`. This way you are manipulating another instance of the list form which is different from the instance which is open and you can see. – Reza Aghaei Aug 05 '16 at 19:06
  • 1
    Instead of creating another instance of your list form, pass the current instance to the constructor of your second form and keep it in a member field and then in save button, call the fill grid method using that instance. Read [this post](http://stackoverflow.com/a/38769212/3110834). Specially this part **Manipulate first Form from second form:** *The first option is useful for this case: You can create a public method or property in first form and pass an instance of the first form to second Form. Then using that method/property on the passed instance, you can manipulate the first form.* – Reza Aghaei Aug 05 '16 at 19:09
  • Thx man I will you are the lifesaver – jhony3 Aug 05 '16 at 19:21
  • @jhony3 I added an example in the answer here. – Reza Aghaei Aug 05 '16 at 19:25

1 Answers1

1

Currently in buttonAccept_Click code, you have created a new instance of the list form and called its FillGrid. This way you are manipulating another instance of the list form which is different from the instance which is open and you can see. You are filling a different form which you didn't show it.

Instead of creating a new instance, create a constructor for your second form which accepts a parameter of first form type. Then when you want to create a new instance of seccod form, pass the instance of the first form (this) to second Form. Then in your save button call the FillGrid method of the passed instance.

For more information about how to manipulate another form, read this post. It contains some useful options about:

  • Pass data to second Form when creating
  • Manipulate second Form after showing
  • Manipulate first Form from second Form

Here is some code which belong to the ListForm:

private void ShowAddForm_Click(object sender, EventArgs e)
{
    if (addForm == null)
    {
        addForm = new AddForm(this);
        addForm.MdiParent = this.ParentForm;
        addForm.FormClosed += AddForm_FormClosed;
    }
    addForm.Show();
}
private void AddForm_FormClosed(object sender, FormClosedEventArgs e)
{
    addForm = null;
}

And here is the code for AddForm

public class AddForm
{
    MyListForm listForm;
    public AddForm(MyListForm f)
    {
        InitializeComponent();
        listForm = f;
    }
    private void SaveVutton_Click(object sender, EventArgs e)
    {
        //perform validation and save data
        f.FillGrid();
    }
}
Community
  • 1
  • 1
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Man thx a lot this works I used to work with `if(form.showdialog()==DialogResult.Yes){ //on first form I fill data }` and on second form I have `if(validation()){this.dialogResult=DialogResult.Yes;}` this is the first on this way – jhony3 Aug 05 '16 at 19:54
  • 1
    You are welcome, I also prefer to use `ShowDialog`. But also the above example is a corrected version of your code :) – Reza Aghaei Aug 05 '16 at 19:55