Search code examples
c#winformsdatagridview

How to pass the highlighted Rows to another DataGridView?


I want to pass the values of the highlighted Rows (dataGridView1) to another DataGridView (dataGridView2). In the figure below (dataGridView1) the rows to be transferred are marked. Many thanks!

private void Button1_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
       int idx = dataGridView1.Columns["A"].Index;

         foreach (DataGridViewCell cell in row.Cells)
         {
            if (row.DefaultCellStyle.BackColor == Color.Red)
            {
                dataGridView2.Rows[idx].Cells[cell.ColumnIndex].Value = cell.Value;
            }

         }
     }

enter image description here

enter image description here


Solution

  • Copy using cell background color:

    private void button1_Click(object sender, EventArgs e)
            {
                dataGridView2.Columns.Clear();
                dataGridView2.Rows.Clear();
                int i;
                Color color2copy = Color.Red;
    
                /*
                dataGridView1.Rows[0].DefaultCellStyle.BackColor = color2copy;
                dataGridView1.Rows[1].DefaultCellStyle.BackColor = color2copy;
                */
                
                // create columns
                foreach (DataGridViewColumn c in dataGridView1.Columns)
                {
                    dataGridView2.Columns.Add(c.Clone() as DataGridViewColumn);
                }
                // create columns
    
                // copy cells
                foreach (DataGridViewRow rowx in dataGridView1.Rows)
                {
                    if (rowx.DefaultCellStyle.BackColor == color2copy)
                    {
                        foreach (DataGridViewCell dgvc in rowx.Cells)
                        {
                            if (dgvc.Value != null)
                            {
                                i = dataGridView2.Rows.Add(rowx.Clone() as DataGridViewRow);
                                dataGridView2.Rows[i].Cells[dgvc.ColumnIndex].Value = dgvc.Value;
                            }
    
                        }
                    }
                }
                // copy cells
            }
    

    Copy using selected rows:

    private void button2_Click(object sender, EventArgs e)
            {
                dataGridView2.Columns.Clear();
                dataGridView2.Rows.Clear();
                int i;
    
                // create columns
                foreach (DataGridViewColumn c in dataGridView1.Columns)
                {
                    dataGridView2.Columns.Add(c.Clone() as DataGridViewColumn);
                }
                // create columns
    
                // copy cells
                foreach (DataGridViewRow rowx in dataGridView1.Rows)
                {
                    if (rowx.Selected)
                    {
                        foreach (DataGridViewCell dgvc in rowx.Cells)
                        {
                            if (dgvc.Value != null)
                            {
                                i = dataGridView2.Rows.Add(rowx.Clone() as DataGridViewRow);
                                dataGridView2.Rows[i].Cells[dgvc.ColumnIndex].Value = dgvc.Value;
                            }
    
                        }
                    }
                }
                // copy cells
            }