1
votes

I have a wpf datagrid which allows users to add rows. I need to add a functionality where in a user can copy a data row and on paste it is added as a new row. I am able to copy(Ctrl+C) a row, but on paste (Ctrl+V), all the items are pasted on to the first cell of the row. How can i paste it into each cell of the row.

 <DataGrid Name="grdTest" HorizontalAlignment="Left" Grid.Row="0"  Margin="5,5,0,0" VerticalAlignment="Top" Height="426" Width="1034" 
                  AlternationCount="2" ItemsSource="{Binding TestsList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
 <DataGrid.Columns>
<DataGridCheckBoxColumn Header="Is Active" Binding="{Binding Path=IsActive,Mode=TwoWay}">
  <DataGridTextColumn Width="90">
                                <DataGridTextColumn.Binding>
                                    <Binding Path="TestName" Mode="TwoWay">
                                        <Binding.ValidationRules>
                                            <localVal:ValidationRules/>
                                        </Binding.ValidationRules>
                                    </Binding>
                                </DataGridTextColumn.Binding>
                                <DataGridTextColumn.Header>
                                    <TextBlock Width="80" Text="Test Name" ToolTip=""/>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>

<DataGridTextColumn.Binding>
                                    <Binding Path="TestType" Mode="TwoWay">
                                        <Binding.ValidationRules>
                                            <localVal:ValidationRules/>
                                        </Binding.ValidationRules>
                                    </Binding>
                                </DataGridTextColumn.Binding>
                                <DataGridTextColumn.Header>
                                    <TextBlock Width="80" Text="Test Type" ToolTip=""/>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
 </DataGrid.Columns>
 </DataGrid>

This is what I have got so far:

 private void grdTest_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
        {
            copiedItem = (Test)e.Item;
        }

private void grdTest_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.V && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {

if (App.ViewModel.UpdatedListCommand.CanExecute(null))
                {
                    App.ViewModel.UpdatedListRowCommand.Execute(copiedItem);
                }

}}}

I update the list in the viewmodel and call NotifyProperty changed. The list is updated, however, it doesnt get reflected in the UI. The row ( to which I copy/pasted value) seems empty, but when i select a cell, the value appears. So basically it is being set properly but some how not shown in the datagrid.

3
hi, did you find a solution for that? Can you please update as a answer if you find. I am also searching for a solution on exactly same issue. thanks - Emil

3 Answers

0
votes

I think with Ctrl+C you just copy the content of the row, try to override the function/event that you use when you paste, and create a new datagrid row and insert each data to the right cell. I hope this helps.

0
votes

I don't know if this is the best way, but I did this by inserting the copied item into the ObservableCollection bound to the DataGrid:

private void grdTest_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.V && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
    {
        TestList.Insert(TestList.IndexOf((Test)grdTest.SelectedItem), copiedItem);
    }
}

Note that this pastes a whole new row, rather than filling in an existing blank row.

0
votes

I was able to copy from one row to another using the below code.

    ObservableCollection<Item> allItems;
    private Item copiedItem;
    private void tableInput_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
    {
        //if row itself is copied as opposed to a number
        if(e.Item is Item)
            copiedItem=(Item)e.Item;
    }

    private void tableInput_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.V && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
        {
            //if row itself is copied
            if(copiedItem!=null) { 
                if(dataGrid.CurrentItem != null)
                {
                    for(int i=0;i<allItems.Count;i++)
                    {
                        if(allItems[i].Equals(dataGrid.CurrentItem))
                        {
                            allItems[i]=DeepClone<Item>(copiedItem);
                        }
                    }
                }
            }
        }
    }


    [Serializable]
    public class Item
    {
        public string Name {get;set; }
        public string Max_Flow {get;set; }
    }

    public static T DeepClone<T>(T obj)
    {
        using (var ms = new MemoryStream())
        {
            var formatter = new BinaryFormatter();
            formatter.Serialize(ms, obj);
            ms.Position = 0;
            return (T)formatter.Deserialize(ms);
        }
    }

My code doesn't create new rows but if you replace

allItems[i]=DeepClone<Item>(copiedItem);

with

allItems.Add(new Item() {Name=<something>,Max_Flow=<something> });

I think it will do what you need.