How to select information from multiple Datagridview C#lines

Asked

Viewed 747 times

3

I’ve been messing with C# lately and I’m having a hard time. I want to get information from several selected lines on DataGridView, and, depending on the number of lines, pass this information to forms that will be displayed, provided that, if two lines are selected, two forms will be created (each with the information of each row that was selected), and so on. The problem is that if I select a line, everything is fine, but when I select more lines, it always opens the forms with the information of the last line that was selected.

How to select the right information for each row?

Follows the Code:

private void button1_Click_1(object sender, EventArgs e)
{
      string email, nome;

      foreach (DataGridViewRow row in dgvEmpresas.SelectedRows)
      {
          email = dgvEmpresas.CurrentRow.Cells[2].Value.ToString();
          nome = dgvEmpresas.CurrentRow.Cells[1].Value.ToString();
          trat = dgvEmpresas.CurrentRow.Cells[3].Value.ToString();


          frmEmail f = new frmEmail();
          f.MdiParent = this.MdiParent;
          f.Show();

          f.txtEnviarPara.Text = email;
          f.lblTratamento.Text = trat;
          f.cboEmpresas1.Text = nome;
      }
}

1 answer

4

Apparently a mistake in the foreach:

When you traverse the datagrid lines, each analyzed row is being placed in the variable row. But when reading the value, you’re reading from dgvEmpresas.CurrentRow.

Your code should look like this:

private void button1_Click_1(object sender, EventArgs e)
{
      string email, nome, trat;

      foreach (DataGridViewRow row in dgvEmpresas.SelectedRows)
      {
          email = row.Cells[2].Value.ToString();
          nome = row.Cells[1].Value.ToString();
          trat = row.Cells[3].Value.ToString();


          frmEmail f = new frmEmail();
          f.MdiParent = this.MdiParent;
          f.Show();

          f.txtEnviarPara.Text = email;
          f.lblTratamento.Text = trat;
          f.cboEmpresas1.Text = nome;
      }
}

As a personal suggestion for the code to be cleaner and more understandable, I would create a frmEmail() with the minimum data you need, and pass the data obtained from the datagrid via constructor. It would look like this:

I would create a constructor like this in frmEmail

public frmEmail(Form mdiParent, string emailDestino, string nomeEmpresa, string tratamento)
{
    InitializeComponent();

    this.MdiParent = mdiParent;

    this.txtEnviarPara.Text = emailDestino;
    this.cboEmpresas1.Text = nomeEmpresa;
    this.lblTratamento.Text = tratamento;
}

And in your click event would look like this:

private void button1_Click_1(object sender, EventArgs e)
{
    foreach (var row in dgvEmpresas.SelectedRows)
    {
        var emailDestino = row.Cells[2].Value.ToString();
        var nomeEmpresa = row.Cells[1].Value.ToString();
        var tratamento = row.Cells[3].Value.ToString();

        var formEmail = new frmEmail(this.MdiParent, emailDestino, nomeEmpresa, tratamento);
        formEmail.Show();
    }
}

It’s just a suggestion. I hope I’ve helped.

  • 1

    It worked! Thank you very much, Max!

  • 1

    Oops, I’m happy. Mark as answered ae. Abs and good programs!!

Browser other questions tagged

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