Remove spaces when pasting into Excel from DataGridView

1

I want to paste the contents of a DataGridView in Excel. However, the content of a column, because each row has extra blanks at the end of the string, makes this column much longer than the content.

Whereas I have this code to select what I want to paste:

    dgv1.SelectAll()
    dgv1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText
    Clipboard.SetDataObject(dgv1Operaciones.GetClipboardContent())
    Sheet.Range("A5").Select() 
    Sheet.Paste() 

How can I apply Trim () in this case, so that the affected column stops lengthening more than it should? Thanks in advance.

    
asked by KPavezC 14.06.2017 в 16:20
source

1 answer

1

Taking your code into account, this would be a way to do it.

In the end, the replace what you do is replace the spaces for nothing.

dgv1.SelectAll()
                dgv1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText.ToString().replace(" ", "");
                Clipboard.SetDataObject(dgv1Operaciones.GetClipboardContent())
                Sheet.Range("A5").Select() 
                Sheet.Paste() 
    
answered by 15.06.2017 в 00:39