I have a WPF datagrid that is linked to a database. Some of the text fields are mapped to Nvarchar(255) fields in the database. I need to be able to copy the entire datagrid and paste it in Excel. I am currently using the following code to achieve this:

    Dim VacancyGrid = Me.OMRStocklistsDataGrid
    VacancyGrid.SelectAllCells()
    ApplicationCommands.Copy.Execute(Nothing, VacancyGrid)

This works fine except that when the string / text fields are pasted in Excel (using paste special - csv) they are all 255 characters long. Most of the actual fields only contain 20 or so characters of text and then whitespace is added to make exactly 255 characters.

How can I trim this extra whitespace either in the datagrid or when copying to the clipboard?

Alternatively if there is a better way of achieving a smooth transition from a datagrid to an excel file, that would be great to know.

Thanks very much!

有帮助吗?

解决方案

Okay,

The answer is to trim upstream - at the database end. I ended up using a view:

RTRIM (Your field)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top