Add data from one DataGridView to another from another form

0

I am making an application that allows me to loan several equipment or tools, so I have created a loan form that registers the user and another form that searches for the product in question. The data that I would like to send would be the id of the product that is hidden, the name of the product and the quantity available.

The forms would be as follows.

Where I get the data.

That would be where I would receive them.

To retrieve the data from the database I use the following code.

public DataTable Buscar()
{
    string ComandoSQL = "SELECT producto.id_prod, producto.nomb_prod, producto.precio_producto, producto.cantidad, producto.descripcion_prod, producto.id_categoria, categoria.nomb_categoria, producto.id_marca, marca.nomb_marca FROM producto, marca, categoria WHERE producto.id_categoria = categoria.id_categoria AND producto.id_marca = marca.id_marca";
    return MiConexion.EjecutarSentencia(ComandoSQL);
}

And to retrieve the GridView data I use the following.

private void RecuperarDatos(object sender, DataGridViewCellMouseEventArgs e)
{
    int fila = e.RowIndex;
    id_producto = dtg_Busqueda.Rows[fila].Cells["id_producto"].Value.ToString();
    nombre = dtg_Busqueda.Rows[fila].Cells["nomb_producto"].Value.ToString();
    cantidad = dtg_Busqueda.Rows[fila].Cells["cantidad"].Value.ToString();

    //enviar ok para que ventana padre pueda recibir
    this.DialogResult = DialogResult.OK;
    this.Dispose();
}

My question is, how do I put the recovered data in a new GridView in another form?

    
asked by GreedSource 27.03.2018 в 23:25
source

1 answer

2

I have an article I do not explain exactly on this topic

Communicate Forms

The idea is that you create an interface to decouple the communication between the forms and you can invoke a method in the parent form, passing data from the child form (in this case the search form)

NEVER access the controls of the parent form instance because you generate coupling, invoke a defined method in the interface sending the data of the selected product, as explained in the article.

The idea is that you define an interface like being

interface IForm{
   void Metodo1(int idproducto);
}

Then you have the parent form implement the interface

public class FormPrincipal : Form, IForm {
    //codigo del form

    //implementas el metodo de la interfaz que recibe el producto
}

When you invoke the form it passes the instance in the constructor

private void btnBuscar_Click(object sender, EventArgs e)
{
    frmBuscar obj = new frmBuscar(this);
    obj.Show();      
}

In the formbuscar you would use

 public class frmBuscar : Form{

     private IForm _form;

     public frmBuscar(IForm form){ //este es el constructor
          _form = form;
     }

     //para acceder al form principal lo haces por medio de _form
     //donde llamarias al metodo de la interfaz
 }
    
answered by 28.03.2018 / 05:46
source