Pass variable to another form, dynamically generated C #

3

I have a relative question of passing the value from one 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 happens in the loop, it shows the values correctly, but when clicking, it sends to print the last result of the loop;

    
asked by anonymous 08.12.2015 / 12:27

2 answers

1

You are overwriting the value of variable sexo and tipo . If you want to pass the list of results you have to popular 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 you populate the variables you change the code by 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 form Imprimir you also create a list of the same type that you just popped up, then just assign:

imprimir.lista = dados;
    
08.12.2015 / 12:48
0

You can pass in the initialization of the other form.

Form2 form2 = new Form2(listaDeDados);
    
17.10.2016 / 15:06