How to send a record of a DataGridView cell to a textbox of another form?

0

What I want to do is that when I click modify, send me another form to edit and the data appear in the textbox, but I do not know what I'm doing wrong ?. There is the datagridview code

 private void dgvDocente_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            Agregardocente vern = new Agregardocente();
            vern.txtnombred.Tag = Convert.ToInt32(dgvDocente.Rows[e.RowIndex].Cells["IdDocente"].Value.ToString());
            Agregardocente verno = new Agregardocente();
            verno.txtnombred.Text = dgvDocente.Rows[e.RowIndex].Cells["Nombre"].Value.ToString();
            Agregardocente vera = new Agregardocente();
            vera. txtapellidod.Text = dgvDocente.Rows[e.RowIndex].Cells["Apellidos"].Value.ToString();
            if (dgvDocente.Rows[e.RowIndex].Cells["Cedula"].Value == null)
            {
                Agregardocente verc = new Agregardocente();
                verc.txtcedulad.Text = "";
            }

            else
            {
                Agregardocente verc = new Agregardocente();
                verc.txtcedulad.Text = dgvDocente.Rows[e.RowIndex].Cells["Cedula"].Value.ToString();
            }
            Agregardocente veres = new Agregardocente();
            veres. txtespecialidadd.Text = dgvDocente.Rows[e.RowIndex].Cells["Especialidad"].Value.ToString();
            Agregardocente verd = new Agregardocente();
            verd.txtdirecciond.Text = dgvDocente.Rows[e.RowIndex].Cells["Direccion"].Value.ToString();
            if (dgvDocente.Rows[e.RowIndex].Cells["Telefono"].Value == null)
            {
                Agregardocente vert = new Agregardocente();
                vert.txttelefonod.Text = "";
            }
            else
            {
                Agregardocente vert = new Agregardocente();
                vert.txttelefonod.Text = dgvDocente.Rows[e.RowIndex].Cells["Telefono"].Value.ToString();
            }
        }



        ActualizarLista();
    }

    
asked by Johardy Zamora 16.11.2017 в 20:46
source

1 answer

0

I do not understand the code very well, is the class of your form second? I do not understand why you create several instances of that type, and I do not see where you send your form, something like:

vern.ShowDialog();

What I would do in your case is to create a class that has all the fields you need in your form:

public class Docente
{
    public string Nombre {get;set;}
    public string Apellido {get;set;}
    public string Cedula {get;set;}
    public string Especialidad {get;set;}
    public string Direccion {get;set;}
    public string Telefono {get;set;}
}

Then, to your form I would add a property of that type and initialize it in the constructor:

public partial class Agregardocente : Form
{
    public Agregardocente()
    {
        Dato = new Docente();
    }
    .
    .
    .
    public Docente Dato {get;set;}

And in the Load, you fill your Textbox with the data of that property:

private void Agregardocente_Load(object sender, EventArgs e)
{
    textbox1.Text = Dato.Nombre;
    textbox2.Text = Dato.Apellido;
    // etc...
}

and in the click event of the save button, you replace the values of that Data property with those of the textbox:

private void guardarbtn_Click(object sender, EventArgs e)
{
    Dato.Nombre = textbox1.Text;
    Dato.Apellido = textbox2.Text;
    // etc.
}

And in the dgvDocente_CellClick method that you set as an example, instead of all those calls, you do that:

private void dgvDocente_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0)
    {
        Agregardocente vern = new Agregardocente();
        vern.Dato.Nombre = dgvDocente.Rows[e.RowIndex].Cells["Nombre"].Value.ToString();
        vern.Dato.Apellido = dgvDocente.Rows[e.RowIndex].Cells["Apellidos"].Value.ToString();
        // etc
        if (vern.ShowDialog() == DialogResult.OK)
        {
             // aquí ya puedes obtener los cambios que se hicieron en el formulario en la propiedad Dato
             dgvDocente.Rows[e.RowIndex].Cells["Nombre"].Value = vern.Dato.Nombre;

That yes, I do not know how you have made your forms.

    
answered by 17.11.2017 / 01:59
source