Is there a simplest way to copy rows in one datagrid and paste it to another
It is simple in Winform. How to simply do it in WPF?
Code:
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
Re: Is there a simplest way to copy rows in one datagrid and paste it to another
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
Re: Is there a simplest way to copy rows in one datagrid and paste it to another
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.
Re: Is there a simplest way to copy rows in one datagrid and paste it to another
Heyy,
First you have to declare DataGrid like this:
Code:
<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
Code:
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!!
Re: Is there a simplest way to copy rows in one datagrid and paste it to another
Quote:
Originally Posted by
HarshShah
Heyy,
First you have to declare DataGrid like this:
Code:
<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
Code:
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!!
What are dataGrid1_CopyingRowClipboardContent and ValidatePaste()?