Home > Blockchain >  Can't insert data in the same previous form - I lose data when I create a new form
Can't insert data in the same previous form - I lose data when I create a new form

Time:02-23

My problem is that when I choose a row in my datagridview, it opens a new form, not the previous one.

Here is my code:

Button: "choix de l'article" in "Form1" : f2 to call form2

public partial class A_commande : Form
{
    public A_commande()
    {
        InitializeComponent();
    }

    private void button12_Click(object sender, EventArgs e)
    {
        A_fournisseur_article f2 = new A_fournisseur_article();

        f2.Show();
    }
}

Form2: f1 to call form1

private void articleDataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
        if (e.RowIndex >= 0)
        {
            A_commande f1 = new A_commande();
            DataGridViewRow row = this.articleDataGridView.Rows[e.RowIndex];

            f1.articleComboBox.Text = articleDataGridView.CurrentRow.Cells[0].Value.ToString();
            f1.catégorieTextBox.Text = articleDataGridView.CurrentRow.Cells[1].Value.ToString();

            this.Hide();
            f1.Show();
        }
}

Form1

Form2

(My problem): it's not "form1" but I get "new form1"

Thank you so much for your time.

CodePudding user response:

You create a new form each time, when you need to access the existing one.

It is a bit like buying a new car each time you go to work, when instead you should just keep the keys and use the same car again.

So you need to change your 2nd form to accept a reference to your first when when you create it. Then you use this reference.

First do this in your second form

public partial class A_fournisseur_article : Form
{
    //You need somewhere to store the reference to your first form
    private A_commande firstForm = null;

    private A_fournisseur_article ()   //Change this to private
    {
        InitializeComponent();
    }

    //create a new public constructor, which acepts the reference
    public A_fournisseur_article (A_commande myFirstForm) :  this()  //make sure it call this() which is the constructor above.
    {
        //Now we store the reference
        this.firstForm = myFirstForm;
    }


    private void articleDataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            //Remove the line below.  We don't want a new one!
            //A_commande f1 = new A_commande();

            DataGridViewRow row = this.articleDataGridView.Rows[e.RowIndex];

            //Now you use your reference from earlier
            firstForm?.articleComboBox.Text = articleDataGridView.CurrentRow.Cells[0].Value.ToString();
            firstForm?.catégorieTextBox.Text = articleDataGridView.CurrentRow.Cells[1].Value.ToString();

            this.Hide();
            f1.Show();
        }
    }
}

Now you need to change how you create and call your second form from the first

public partial class A_commande : Form
{
    public A_commande()
    {
        InitializeComponent();
    }

    private void button12_Click(object sender, EventArgs e)
    {
        //Here, we now pass a reference to this form (A_commande)
        A_fournisseur_article f2 = new A_fournisseur_article(this);

        f2.Show();
    }
}
  • Related