We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to paste the copied row in AddNewRow in WPF DataGrid (SfDataGrid)?

Platform: WPF |
Control: SfDataGrid

You can paste the copied row in AddNewRow by overriding the PasteTextToRow method in GridCutCopyPaste class in WPF DataGrid (SfDataGrid).

XAML

<syncfusion:SfDataGrid x:Name="sfdatagrid"
                               AddNewRowPosition="Top"
                               AllowEditing="True"
                               AutoGenerateColumns="True"
                               GridCopyOption="CopyData"
                               ItemsSource="{Binding EmployeeDetails}"
                               SelectionMode="Extended"
                               SelectionUnit="Row" />

C#

this.sfdatagrid.GridCopyPaste = new CustomPaste(sfdatagrid);
…
public class CustomPaste : GridCutCopyPaste
{
    public CustomPaste(SfDataGrid sfgrid)
        : base(sfgrid)
    {
 
    }
 
    protected override void PasteTextToRow()
    {
        IDataObject dataObject = null;
        dataObject = Clipboard.GetDataObject();
        var clipBoardContent = dataObject.GetData(DataFormats.UnicodeText) as string;
        string[] records = Regex.Split(clipBoardContent.ToString(), @"\r\n");
        if (dataGrid.SelectionUnit == GridSelectionUnit.Row)
        {
            dataGrid.Focus();
            bool isAddNewRow= this.dataGrid.IsAddNewIndex(this.dataGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.RowIndex);
               
            //Checking for row is AddNewRow.
            if (isAddNewRow)
            {
                if (!this.dataGrid.View.IsAddingNew)
                    return;
                                      
                string[] record = Regex.Split(records[0], @"\t");
                var provider = this.dataGrid.View.GetPropertyAccessProvider();                  
                var rowData = (this.dataGrid.View as CollectionViewAdv).CurrentAddItem;
 
                //Paste the copied row in each cell.
                foreach (var column in this.dataGrid.Columns)
                {                       
                    var cellText = provider.GetValue(rowData, column.MappingName);
                    if (cellText == null)
                    {
                        PropertyDescriptorCollection typeInfos = this.dataGrid.View.GetItemProperties();
                        var typeInfo = typeInfos.GetPropertyDescriptor(column.MappingName);                          
                    }
                    CommitValue(rowData, column, provider, record[this.dataGrid.Columns.IndexOf(column)]);
                }
            }
        }
        else
            base.PasteTextToRow();
    }
}        

 

Note:

You can only paste the copied row after initiating the AddNewRow by editing.

View sample in GitHub.

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile