-
Jun 24th, 2017, 04:01 AM
#1
Thread Starter
Junior Member
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
-
Jun 25th, 2017, 11:44 AM
#2
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
-
Jun 26th, 2017, 10:11 AM
#3
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.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Feb 20th, 2020, 11:38 PM
#4
Member
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!!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|