jhony3
jhony3

Reputation: 272

How can I do data synchronization between two child form inside parent container winforms?

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();
    }

Upvotes: 1

Views: 524

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125217

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();
    }
}

Upvotes: 1

Related Questions