How to pass data in DataGridView to Other DataGridView?

2

I have two DataGridView in this way:

What I need is that the CheckBox that are selected from the first DatagridView when pressing the Button "add" are passed to the second DataGridView .NOTA: The total of the second DataGridView I will calculate it from column cantidad and precio (which is hidden). I really already try in some ways with the little knowledge I have, but I think I am far from getting it, so I hope you can help me thanks.

foreach (DataGridViewRow row in dgvInsumo.Rows)
            {
                try
                {
                    if (Convert.ToBoolean(row.Cells[1].Value))
                    {
                        foreach (DataGridViewRow row2 in dgvInsumosAgregados.Rows)
                        {
                            dgvInsumosAgregados[0, row2.Index].Value = dgvInsumo[0, row.Index].Value;
                            dgvInsumosAgregados[1, row2.Index].Value = dgvInsumo[2, row.Index].Value;
                            dgvInsumosAgregados[2, row2.Index].Value = dgvInsumo[3, row.Index].Value;
                            dgvInsumosAgregados[3, row2.Index].Value = dgvInsumo[4, row.Index].Value;                            
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Sistema Restaurante", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

This was the last way I tried, the most likely thing is that it's very bad. Thank you already shows me something at least but it does it this way:

Probably because the columns of both DataGridView are not in the same order or with the same name.

    
asked by U.C 31.07.2017 в 17:22
source

2 answers

2

You are very close to the solution. What you should do is add a new DataGridViewRow to your second DataGridView :

foreach (DataGridViewRow row in dgvInsumo.Rows)
{
     try
     {
          if (Convert.ToBoolean(row.Cells[1].Value))
          {
               dgvInsumosAgregados.Rows.Add(row.Cells[0].Value,
                                            row.Cells[2].Value, 
                                            row.Cells[3].Value, 
                                            row.Cells[4].Value)                           
          }

     }
     catch (Exception ex)
     {
           MessageBox.Show(ex.ToString(), "Sistema Restaurante", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
}

Here DataGridView.Rows you have an example of how to add and insert new rows to a DataGridView

    
answered by 31.07.2017 / 18:02
source
1

I could already, I just put the number of celda of the second DataGridView like this:

foreach (DataGridViewRow row in dgvInsumo.Rows)
            {
                try
                {
                    if (Convert.ToBoolean(row.Cells[1].Value))
                    {                       

                        DataGridViewRow fila = new DataGridViewRow();
                        fila.CreateCells(dgvInsumosAgregados);
                        fila.Cells[0].Value = row.Cells[0].Value;
                        fila.Cells[1].Value = row.Cells[2].Value;
                        fila.Cells[2].Value = row.Cells[4].Value;
                        fila.Cells[4].Value = row.Cells[3].Value;
                        dgvInsumosAgregados.Rows.Add(fila);
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Sistema Restaurante", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

Thanks to everyone for answering they were very helpful.

    
answered by 31.07.2017 в 18:58