0 votes

Pass data from a datagrid in form2 to text fields in form1, C# with Visual Studio 2017

I have a Form1 which has 4 text fields: txt_id , txt_rut , txt_dv , txt_razonSosial . And a button that calls a Form2 . In this Form2 what I do is load in DataGrid with the information of some company names and their routes.

My idea is that when I double click on some data in the row where the company name I am looking for is located, these data will be sent to my 4 fields of the Form1 .

I saw that there is something similar in the forum in this one. link but when reading the code I don't understand the answer when they say:

As you can see, you receive in the Constructor a TextBox If you modify it, it will be modified in the original form.

For me, in this example, the Form1 is where the child has the text fields and the form2 has its list.

This is what I have in my Form2 :

namespace PasoDato
{
    public partial class Form2 : Form
    {

        // variables
        public static SqlConnection conexion = new SqlConnection();
        public static SqlCommand comando = new SqlCommand();

        public static void AbrirConexion()
        {
            string cadena_conexion = @"Data Source=********;Initial Catalog=*****;Integrated Security=False; user id=****;password=*****";
            SqlCommand comando = new SqlCommand();
            conexion.ConnectionString = cadena_conexion;
            conexion.Open();
        }

        public static void CerrarConexion(){conexion.Close();}

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            AbrirConexion();
            comando.Connection = conexion;
            comando.CommandText = "select IdCliente, Rut, Dv,RazonSocial from zf_Clientes order by RazonSocial";
            SqlDataReader reader = comando.ExecuteReader();

            while (reader.Read())
            {
                dataGridView1.Rows.Add(reader["IdCliente"].ToString(), reader["Rut"].ToString(), reader["Dv"].ToString(), reader["RazonSocial"].ToString());

            }
            reader.Close();
            CerrarConexion();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var dataIndexNo = dataGridView1.Rows[e.RowIndex].Index.ToString();
            string cellValue = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            string id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            string rut = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            string dv = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            string razonSocial = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();

            MessageBox.Show("id: "+ id + " Rut: "+rut+" Razón Social: "+razonSocial);

            Form1 datos = new Form1();
            datos.id = id;
            datos.rut = rut;
            datos.dv = dv;
            datos.razon = razonSocial;

            this.Close();
        }
    }
}

I already capture the information I need and display it with an MessageBox now what I don't understand with the example of the link is to send them to the Form1 to display them in the TextBox I have.

I have this to test, but it doesn't work:

namespace PasoDato
{
    public partial class Form1 : Form
    {

        public string id = "";
        public string rut = "";
        public string dv = "";
        public string razon = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 formu = new Form2();
            formu.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if(id != "")
            {
                txt_id.Text = id;
            }
        }
    }
}

If someone can give me some guidance I would appreciate it.

Thank you very much

1voto

What I do to link data from different forms.

FORM 1

 public partial class Form1 : Form
        {
            public static  Form1 instance=null;
            public string id = "";
            public string rut = "";
            public string dv = "";
            public string razon = "";

            public Form1()
            {
                InitializeComponent();
                instance = this;

            }

FORM 2:

In this way you access to the controls and public variables of the form 1 (this you can do it from any part of the application), you can access or modify, you simulate a reactive process. *Remember that to access to the controls of Windows form, as for example a grid this must be in a public way.

Form1.instance.id 
Form1.instance.rut
Form1.instance.dv 
Form1.instance.razon

0voto

Martin Fernandez Points 112

This is what I would do

In the Form with the control to be modified I create this function

public void EditarTxt(string _texto)
{
  textbox1.Text = _texto;
}

In the form where I want to manipulate the other form I do this function

Class form2
{      

   Form1 frm = new Form();

    void Llamarfunciondelotroform()
    {          
      frm.EditarTxt("texto");
      frm.Show();
    }
}

Explanation

I make a function that manipulates the control that is on that same Form . And then from the Form2 I create a new instance of the Form1 that I want to display with the text I want to enter in the TextBox from Form1 calling the function I made.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X