# .NET and More > WPF, WCF, WF >  Is there a simplest way to copy rows in one datagrid and paste it to another

## selfay

It is simple in Winform. How to simply do it in WPF? 




```
For Each DRow As DataGridViewRow In Datagrid1.SelectedRows
Dim ClnRow As DataGridViewRow = CType(DRow.Clone(), DataGridViewRow)

For i As Int32 = 0 To DRow.Cells.Count 'can decide how many cells will be copied
ClnRow.Cells(i).Value = DRow.Cells(i).Value
Next

datagird2.Rows.Add(ClnRow)
Next
```

----------


## KGComputers

I don't know if there's an existing method in .NET  to just clone a WPF DataGrid right away. From what I've read you can use XamlWriter as an option for that but it has pitfalls according to some. How about extracting the items(record) from the Grid and put it in a List or Observable Collection and then bind it to the second grid?

- kgc

----------


## Sitten Spynne

If you're using a WPF DataGrid, you should be using MVVM and putting objects inside of it. So "copying a row" is "copying the objects". If you're doing anything else, you're making it harder on yourself.

----------


## HarshShah

Heyy,
First you have to declare DataGrid like this:



```
<DataGrid Name="dataGrid1" ItemsSource="{Binding PeopleList}" 
                  SelectionUnit="Cell"
                  AutoGenerateColumns="True" 
                  KeyDown="dataGrid1_KeyDown" 
                  CopyingRowClipboardContent="dataGrid1_CopyingRowClipboardContent" 
                  ColumnReordered="dataGrid1_ColumnReordered"/>
```

SelectionUnit="Cell" This allows individual cells to be selected rather than the whole row.
The KeyDown event is set to detect the paste operation.

VIEW MODEL


```
void dataGrid1_KeyDown(object sender, KeyEventArgs e)
{
  if (e.Key == Key.V &&
      (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
  {
    if (ValidatePaste() == false)
      return;

    viewModel.Paste(dataGrid1.CurrentColumn.DisplayIndex,
         GetTargetRow(),
         dataGrid1.SelectedCells.Count,
         Clipboard.GetData(DataFormats.Text) as string);
  }
}
```

Try this code!!

----------

