none
CTRL-C on datagrid - how to remove line feed when pasting

    Question

  • It appears that the default behavior for copying (read-only) cells from a datagrid is to include a final line feed. Users then need to delete the line feed after pasting. I've tried to manipulate the clipboard contents, with no luck. How can I remove the line feed?

    private void DataGrid_OnCopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
            {
    
    e.ClipboardRowContent...?
    }
    Technically, I only want the first column data, and I'm able to achieve that, but there is always a line feed.

    Thursday, March 8, 2018 3:33 PM

All replies

  • Hi,

    I don't know, how do you copy and paste.

    But you can  get Clipboard Data with remove the specific  characters as below.

     string ClipboardData=((string)Clipboard.GetData(DataFormats.Text)).TrimEnd('\r', '\n');

    Then you can set data for the Clipboard  as below,

    Clipboard.SetData(DataFormats.Text, ClipboardData);

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 9, 2018 2:56 AM
    Moderator
  • Clipboard.SetData is ignored when copying from a datagrid.

    The only object you have to work with is:

    e.ClipboardRowContent

    where e is DataGridRowClipboardEventArgs

    XAML...

    <DataGridCopyingRowClipboardContent="DataGrid_OnCopyingRowClipboardContent"...

    Thursday, March 15, 2018 2:10 PM
  • Hi RLuther,

    Sorry for my late reply.

    Try following code:

     private void DataGrid_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (DataGridClipboardCellContent c in e.ClipboardRowContent)
                {
                    stringBuilder.Append(c.Content);
                }
                e.ClipboardRowContent.Clear();
                e.ClipboardRowContent.Add(new DataGridClipboardCellContent(e.Item, (sender as DataGrid).Columns[0], stringBuilder));
            }

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 20, 2018 8:39 AM
    Moderator
  • Thank you, unfortunately this still does not work. I used your exact code.
    Wednesday, April 18, 2018 2:01 PM