Value not escaped when single cell is copied to the clipboard from DataGridView as CSV - by arhughes

Status : 

 


2
0
Sign in
to vote
ID 835602 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 3/17/2014 10:43:38 AM
Access Restriction Public

Description

When a single cell is copied from the DataGridView escaping is not applied to the value insert into the clipboard as CSV. If more than one cell is copied the values are escaped properly.

See: http://referencesource.microsoft.com/#System.Windows.Forms/ndp/fx/src/winforms/Managed/System/WinForms/DataGridViewCell.cs#1584

If the cell is the first and last cell, and is in the first and last row (i.e. it is the only cell) no escaping is applied. This is a problem if the value in the cell contains text that must be escaped (like a comma). For example if the cell contains the value "$1,000" it will not be quoted in the CSV and any tool that parses CSV from the clipboard will parse the data as two cells: "$1" and "000".

Note that copying a single cell with the value "$1,000" will produce exactly the same output on the clipboard as copying two cells with the values "$1" and "000".
Sign in to post a comment.
Posted by Microsoft on 3/21/2014 at 10:42 PM
Thank you for reporting this issue. Customer feedback is a critical part of a successful, impactful software product. Unfortunately another part is the reality of schedules and the need to prioritize investments according to the objectives of the product. We have evaluated the issue that you have reported and at this point in the product's lifecycle, it does not meet the criteria to be addressed. This evaluation is carefully done and considers many aspects including the cost of the fix, implications of the change, and the number of reported instances of the issue.
 
To work around the issue you can define a custom cell class which would override the problematic method, GetClipboardContent, which you had pointed out :
public class MyCell : DataGridViewTextBoxCell
    {
     protected override object GetClipboardContent(int rowIndex,
                                                     bool firstCell,
                                                     bool lastCell,
                                                     bool inFirstRow,
                                                     bool inLastRow,
                                                     string format)
        {
            object o = base.GetClipboardContent(rowIndex, firstCell, lastCell, inFirstRow, inLastRow, format);
            if (String.Equals(format, DataFormats.CommaSeparatedValue, StringComparison.OrdinalIgnoreCase))
            {
                if (firstCell && lastCell && inFirstRow && inLastRow)
                {
                    string s = o.ToString();
                    if (s.IndexOf(',') != -1)
                    {
                        o = string.Format("\"{0}\"", s);
                    }
                }
            }
            return o;
        }
    }
And use that new cell class like this:
            c = new DataGridViewColumn(new MyCell());
            c.HeaderText = "My Column";
            this.dataGridView1.Columns.Add(c);


Many customers have found it useful to discuss issues like this in the forums (http://www.microsoft.com/communities/forums/default.mspx) where Microsoft and other members of the community can recommend ways of achieving the behavior you are interested in.

Thank you,
The Windows Forms Product Team
Posted by Microsoft on 3/18/2014 at 3:49 AM
Thank you for submitting feedback on Visual Studio and .NET Framework. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information. If you require immediate assistance with this issue, please contact product support at http://support.microsoft.com/ph/1117.
Posted by Microsoft on 3/17/2014 at 6:36 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)