Pass data between forms

2

Language: C Sharp

In the MainForm I have a datagridview whose datasource is a dataset with a datatable .

With a botón the form2 is opened and in this a row number is chosen. When accepting, the form2 must be closed and the number chosen by the user will be used as a row index to eliminate in dataset of Main

I do not know how I can pass this information. I found how to go from form to another using their instance names when creating them but the MainForm runs alone, I do not know what name the instance has.

    
asked by DMolf 09.07.2016 в 03:01
source

1 answer

1

Father Class:

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

    private void btnAbrirHijio_Click(object sender, EventArgs e)
    {
        Hijo hijo = new Hijo(); //Instancia del Hijo
        DialogResult res = hijo.ShowDialog(); //Llamamos nuestra ventana hija a manera de DialogResult
        if (res == DialogResult.OK) //Nos debe regresar un Dialogresult.OK
        {
            string IndiceSeleccionadoDesdeHijo = hijo.IndiceSeleccionado; //Y listo, nos traermos la informacion
        }
    }
}

Daughter Class:

public partial class Hijo : Form
{
    public string IndiceSeleccionado = ""; //Necesitaremos una clase global para obtener los datos desde la clase padre

    public Hijo() //Este es el contructor de la clase
    {
        InitializeComponent();
        btnAceptar.DialogResult = DialogResult.OK; //Esto hara que al dar Click a nuestro boton, este regrese un DialogResult.OK y podamos cacharlo en la clase padre.

    }

    private void btnAceptar_Click(object sender, EventArgs e)
    {

        IndiceSeleccionado = txtIndice.Text; //Asignacion a variable global de la que el Padre extraera el dato

    }
}

If you need help you tell me, but if this is the logic you need friend. Greetings!

    
answered by 09.07.2016 / 03:31
source