Some of my DataGridViewCells return the wrong value in their GetClipboardContent method. They are cells in a DataGridViewComboBoxColumn cells, so they use the displayed property, not the value property of the cell. I want it to return the value itself.
An initial attempt was simply using
Protected Overrides Function GetClipboardContent(ByVal rowIndex As Integer, ByVal firstCell As Boolean, ByVal lastCell As Boolean, ByVal inFirstRow As Boolean, ByVal inLastRow As Boolean, ByVal format As String) As Object
Return Value
End Function
in my DataGridViewComboBoxCell descendant but then I noted that this method is called more than one time per cell value, once for every data format DataGridView supports by standard, which are format=”HTML”, “Text”, “UnicodeText” and “Csv”.
For csv, the base implementation appends a comma if it’s not the last cell, for html it adds the correct tags depending on if it’s the first/last row/cell in the table/table row, etc. I consider this format-specific, not cell-value specific.
So how could I replace the value that ends up in the clipboard without re-implementing all those format-specific aspects? That would result in quite some code for functionality that already exists in the base class, wouldn’t it?
Short answer: Handle
CellFormatting, and if formatting is requested during a clipboard “value fetch”, return the unformatted value instead of the display value. To be able to detect this, let the combobox column create a combobox cell descendant which sets a flag in the column’s properties when itsGetClipboardContentmethod is called.Long answer:
Mh, it seems the base method does something like this (c# code from Mono from http://www.eg.bucknell.edu/~cs208/subpages/software/src/mono-2.6.7/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewCell.cs):
That lets me believe that I should override
GetEditedFormattedValueand check for theClipboardContentbit being set in theContextargument, allowing me to detect if it is fetching the value for the clipboard, and if so, return the Value of the ComboBox cell descendant, not the DisplayValue.GetEditedFormattedValuecannot be overriden, though.So i am forced to create a flag in the column, set it if the cell’s
GetClipboardContentis running, and return the unformatted value of the cell in the Grid’sCellFormattingevent if the flag is set.Works fine.