How to show a row of a datagridview in the textbox of another form? c #

1

what I try to do is take a complete row of a DataGridView and show it in another form, it's for a search button, I want to write the name and when I select it in the gridview and press the button, that data appears in another form . Here is the Datagrid from which I will extract the row:

    string Nombre, Id, fecha;
    double Sueldo, incentivo, result;
    DataGridView filas;
    public Form5(double subru, string nom, string id, double incen, string fech, DataGridView filas, double resultado)
        : this()
    {
        result = resultado;
        Nombre = nom;
        Id = id;
        Sueldo = subru;
        incentivo = incen;
        fecha = fech;
        this.filas = filas;
    }

    private void Form5_Load(object sender, EventArgs e)
    {

    }

    private void button4_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("¿Desea Salir?", "Salir", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == System.Windows.Forms.DialogResult.Yes)
        {
            Application.Exit();
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form1 fd = new Form1(dgv2);
        fd.Visible = true;
        Visible = false;
    }

    private void btndatos_Click(object sender, EventArgs e)
    {
        if (filas != null)
        {
            for (int i = 0; i < filas.Rows.Count - 1; i++)
            {
                DataGridViewRow clonedRow = (DataGridViewRow)filas.Rows[i].Clone();
                for (Int32 index = 0; index < filas.Rows[i].Cells.Count; index++)
                {
                    clonedRow.Cells[index].Value = filas.Rows[i].Cells[index].Value;
                }

                dgv2.Rows.Add(clonedRow);
            }
        }
        dgv2.Rows.Add(fecha, dateTimePicker2, dateTimePicker3, Id, Nombre, Sueldo, incentivo, result);
    }
}

}

    
asked by Rax thegood 19.04.2018 в 01:15
source

1 answer

2

Suppose you have two Form1 and Form2 forms.

  

Note: You must create the datagriedView elements, textboxes and buttons   which I mention in the solution given below, if I refer to a   button inside the code behind the form, you must have created it   (' drag the elements using the tool palette in visual studio ')

Form1: Which has your datagriedview and with which you want to pass the data to Form2 before or after filtering the datagriedView.

using System;
using System.Data;
using System.Windows.Forms;

namespace App
{
    public partial class Form1 : Form
    {
        /*
            Declaramos una variable de tipo datatable para manejar los datos en nuestro datagriedView
         */
        DataTable dt = new DataTable();

        public Form1()
        {
            InitializeComponent();
        }
        /*
            Cargamos los datos a nuestro DatagriedView
         */
        private void Form1_Load(object sender, EventArgs e)
        {

            dt.Columns.Add("id", typeof(int));
            dt.Columns.Add("pais", typeof(string));
            dt.Rows.Add(new object[] { 1, "Belgica" });
            dt.Rows.Add(new object[] { 2, "Francia" });
            dt.Rows.Add(new object[] { 3, "Italia" });
            dt.Rows.Add(new object[] { 4, "España" });
            dt.Rows.Add(new object[] { 5, "Estados Unidos" });
            dataGridView1.DataSource = dt;
        }

        /*
           Evento Click del boton buscar
         */
        private void btnBuscar_Click(object sender, EventArgs e)
        {
            dt.DefaultView.RowFilter = string.Format("pais LIKE '%{0}%'", txtBuscar.Text);
            dataGridView1.DataSource = dt;
        }

        /*
            Evento change del texbox Buscar
         */
        private void txtBuscar_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtBuscar.Text))
            {
                dt.DefaultView.RowFilter = string.Empty;

            }
        }

        /*
            Evento donde tomaremos la fila seleccionada y le pasaremos los datos al formulario 2
         */
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string id = dataGridView1.Rows[e.RowIndex].Cells["Id"].Value.ToString();
            string pais = dataGridView1.Rows[e.RowIndex].Cells["Pais"].Value.ToString();

            Form2 frm2 = new Form2(id, pais);
            frm2.Show();
        }
    }
}

Form2: Which has the fields you want to receive from Form1. In this case, only the ID and the country are received.

using System;
using System.Windows.Forms;

namespace App
{
    public partial class Form2 : Form
    {
        /*
         Estas variables serviran de contenedor para recibir los valores desde el form1
        */
        static string Id;
        static string Pais;

        /*
            Se reciben los valores en el constructor del formulario 2 para asignarle 
            dichos valores a las variables staticas definidas
        */
        public Form2(string id, string pais)
        {
            Id = id;
            Pais = pais;
            InitializeComponent();
        }

        /*
          AL mostrarse el formulario le asignamos los valores de las variables estaticas antes modificadas
       */
        private void Form2_Load(object sender, EventArgs e)
        {

            txtId.Text = Id;
            txtPais.Text = Pais;
        }
    }
}
    
answered by 19.04.2018 в 02:45