Articles in this section
Category / Section

How to copy active cell value alone when row selection in WPF DataGrid (SfDataGrid)?

2 mins read

All the record contents will be copied to the clipboard by default when the SelectionUnit is set as Row in WPF DataGrid (SfDataGrid). You can copy the active cell value alone by using the SfDataGrid.GridCopyCellContent event. You can remove tab character from the copied data by overriding the CopyCell method in GridCutCopyPaste class.

Refer to the following code example to copy the data of active cell alone.

this.AssociatedObject.CopyGridCellContent += AssociatedObject_CopyGridCellContent;
 
private void AssociatedObject_CopyGridCellContent(object sender, GridCopyPasteCellEventArgs e)
{
     //Skip to copy contents for all the inactive cells from the selected row 
     SfDataGrid grid = e.OriginalSender is DetailsViewDataGrid ? (SfDataGrid)e.OriginalSender : (SfDataGrid)sender;
     if (grid != null && grid.SelectionController != null
         && grid.SelectionController.CurrentCellManager != null
         && grid.SelectionController.CurrentCellManager.CurrentCell != null
         && e.Column.MappingName != grid.SelectionController.CurrentCellManager.CurrentCell.GridColumn.MappingName)
     {
           e.Handled = true;
     }
}

 

Refer to the following code example to remove tab character from the copied data.

this.AssociatedObject.GridCopyPaste = new CustomGridCopyPaste(this.AssociatedObject);
 
public class CustomGridCopyPaste : GridCutCopyPaste
{
    public CustomGridCopyPaste(SfDataGrid dataGrid):base(dataGrid)
    {
 
    }
 
    protected override void CopyCell(object record, GridColumn column, ref StringBuilder text)
    {
         base.CopyCell(record, column, ref text);
         text.Replace("\t", string.Empty);
    }
}

 

View sample in GitHub.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied