Pass gridview data (child form) to parent form textbox

1

Hello, good morning everyone, I have the following problem: in FORM_A I have a button from where I call FORM_B, which has a datagridview.

I need to doubleclick a row in the datagridview in FORM_B to pass those values to the textboxes of FORM_A, but it does not do anything about the code I have. This code I have in the grid:

 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

        frmMedicamentosPaciente frm = new frmMedicamentosPaciente();
        DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
        //txtIdMedicamento.Text = row.Cells["idMedicamento"].Value.ToString();  
        //frm1.txtIdMedicamento.Text = row.Cells["idMedicamento"].Value.ToString();
        frm.txtIdMedicamento.Text = row.Cells["idMedicamento"].Value.ToString(); 
    }

and in FORM_A I have the textboxes as publics

Greetings to all and thanks in advance

    
asked by Nicolas Ezequiel Almonacid 23.07.2018 в 17:51
source

1 answer

2

Your problem is based on the following:

  • Form A calls Form B
  • Form B does things. Form B needs to pass things to Form A.
  • Form B needs to know who Form A is.
  • Your code does not tell you who Form A is, but instance a new version of Form A frmMedicamentosPaciente frm = new frmMedicamentosPaciente();

Why is this happening? You are not understanding the difference between a class and an instance of an object. A class is the definition of the class, an instance of a class lives where it was instantiated, and unless it has static properties, an intance does not see the data of another instance.

By doing frmMedicamentosPaciente frm = new frmMedicamentosPaciente(); You are raising another instance of your class frmMedicamentosPaciente (Yes, the forms are classes).

Actually, there are lots of ways to solve this, but a simple one would be to tell Form B, who is form A. To do this, in form B, define the following:

public frmMedicamentosPaciente frmPadre;

And after intact Form B and before opening it, it does something like this:

FormB.frmPadre = this;
    
answered by 23.07.2018 / 18:07
source