Results 1 to 4 of 4

Thread: Is there a simplest way to copy rows in one datagrid and paste it to another

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2016
    Posts
    21

    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

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,022

    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
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    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.

  4. #4
    Member
    Join Date
    Jan 2020
    Posts
    59

    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
  •  



Click Here to Expand Forum to Full Width