Pass data dynamically from one DataGridView to another DataGridView

0

I want to pass the data from a datagridview1 to another datagridview2 by pressing a button, but I want all the selected data to be passed. Until now I only get the records to go through one by one, that is a row. My problem is I can not get all the data through pressing the "pasartodo" button If someone can help me I will be very grateful,

dataGridView1.Rows.Add(new string[] {
    Convert.ToString(dataGridView2[0, dataGridView1.CurrentRow.Index].Value),
    Convert.ToString(dataGridView2[1, dataGridView1.CurrentRow.Index].Value) 
    Convert.ToString(dataGridView1[2, dgvRegistro.CurrentRow.Index].Value) 
});

dataGridView2.Rows.RemoveAt(dataGridView2.CurrentRow.Index);
    
asked by andrea 19.09.2018 в 05:10
source

1 answer

1

You can use foreach to iterate the selected rows using something like this

List<DataGridViewRow> items = new List<DataGridViewRow>();

foreach(var row in dataGridView1.SelectedRows)
{
    items.Add(row);
}

foreach(var item in items)
{
    dataGridView2.Rows.Add(new string[] {
            Convert.ToString(item.Cells[0].Value),
            Convert.ToString(item.Cells[1].Value),
            Convert.ToString(item.Cells[2].Value) 
        });

    dataGridView1.Rows.Remove(item);
}

A temporary list is used because foreach does not allow you to remove while iterating

    
answered by 19.09.2018 в 16:17