How to select multi-row information DataGridView C #

2

I've been messing with C # lately and am having difficulties. I want to get information from multiple selected rows in DataGridView , and, depending on the number of rows, pass this information to forms that will be displayed, as long as two rows are selected, two rows are created (each with line that was selected), and so on. The problem is that if I select a row, that's fine, however, when I select more rows, it always opens the forms with the last line information that was selected.

How do you select the right information for each line?

Follow 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;
      }
}
    
asked by anonymous 04.07.2017 / 16:18

1 answer

0

Apparently an error in foreach :

When you cycle through the rows of the datagrid, each parsed row is being placed in the row variable. But when reading the value, you are 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 constructor in frmEmail() with the minimum of data you need, and pass data 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 it 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 have helped.

    
04.07.2017 / 19:00