Pass variable to another form, dynamically generated C#

Asked

Viewed 955 times

3

I have a question regarding 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 right values, but when clicking, it sends to the print the last result of the loop;

2 answers

1


You are overwriting the value of variable sexo and tipo. If you want to pass the results list 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 exchange the code for it:

// 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 as the one you just popular, then just assign:

imprimir.lista = dados;
  • Thank you. But when I try to recover her values with the data.count. it comes back zeroed

  • The variable you declared globally?

  • Yes, I have globally declared the variables

0

You can pass the other form startup.

Form2 form2 = new Form2(listaDeDados);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.