Pass variable to another form, dynamically generated C#

Question:

I have a question about passing the value of a button to another form.

I have the following code

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {


        string cod_evento = dataGridView1.CurrentRow.Cells[1].Value.ToString();


        codigo_evento = cod_evento;
        string sql = " select * from eventos where cod_evento = " + cod_evento + " ";

        flowLayoutPanel1.Refresh();
        MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);

        DataTable dt = new DataTable();
        da.Fill(dt);
        dataGridView1.Refresh();
        // MessageBox.Show(dt.Rows.Count.ToString());
        if (dt.Rows.Count == 0)
        {
            MessageBox.Show("erro", "erro", MessageBoxButtons.OK);


        }
        else
        {
            int y = 20;
            int incremento = 30;
            int contador = 1;

            for (int i = 0; i < dt.Rows.Count; i++)
            {


               Button b = new Button();

                b.Name = i.ToString();





                b.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                b.Font = new Font("Verdana", 15);
                string sexo = dt.Rows[i]["sexo"].ToString();

                string tipo = string.Concat(dt.Rows[i]["nome_area"].ToString()," - ",sexo);


                b.Text = string.Concat(sexo.ToString(),  tipo.ToString());
                b.Click += new EventHandler(this.b_Click);


                // vamos carregar para o próximo form




                b.Size = new System.Drawing.Size(400, 60);





                flowLayoutPanel1.Controls.Add(b);
                flowLayoutPanel1.AutoScroll = true;
                y = y + incremento;
                contador++;
                lblCarregando.Text = "";

            }
        }

    }


      void b_Click(object sender, EventArgs e)
    {


        Imprimir imprimir = new Imprimir();


        imprimir.sexo = sexo;
        imprimir.tipo = tipo;

        imprimir.cod_evento = codigo_evento;

        // vamos carregar o form com os tipos e quantidades



        imprimir.Show();
        lblCarregando.Text = "";
    }

But it turns out that in the loop, it shows the values ​​just right, but when you click, it sends the last loop result to the printout;

Answer:

You are overwriting the value of the sexo and tipo variable. If you want to pass the results list you have to populate a collection and not a variable. I suggest you create a class.

public class Evento
{
    public string Sexo;
    public string Tipo;
}

Then a list:

List<Evento> dados = new List<Evento>();

Within the method that you populate the variables, you change the code for this:

// código omitido
string sexo = dt.Rows[i]["sexo"].ToString();
string tipo = string.Concat(dt.Rows[i]["nome_area"].ToString()," - ",sexo);

dados.Add(new Evento {
    Sexo = sexo,
    Tipo = tipo
});

In your Imprimir form you also create a list of the same type as the one you just popularized, then just assign:

imprimir.lista = dados;
Scroll to Top