When selecting a cell in TStringGrid the entire cell contents are selected, with the cursor positioned at the end of the selection. This is a problem.
My grid is being populated via a Live Binding to a TADOConnnection to a FoxPro database, I have a TADODataSet with FieldDefs that include the Size and Datatype of the fields ,for instance:
object DSProtocol: TADODataSet
Connection = CONProtocol
CursorType = ctStatic
LockType = ltBatchOptimistic
CommandText = 'select jobc from protocol'
FieldDefs = <
item
Name = 'jobc'
Attributes = [faFixed]
DataType = ftFixedChar
Size = 200
end>
end
I am unable to change the underlying database schema. In most cases the fields are way oversized for the information stored, the largest length value stored in jobc is 8 characters long, and will probably never be more than 10.
For the UI then, I have no reason to size the column holding jobc to show 200 characters, I just need it big enough to show 10. However, the TStringGrid cell is filled with the contents of that field plus enough space characters to pad it to 200. (Why? I don't know.) When selecting, the entire contents are selected, the cursor is placed at the end of the selection, and the end result is that it looks like the contents disappeared.
It does not seem like I can use any of the TSTringGrid events to override this behavior.
I attempted a quick and dirty fix of adding the (deprecated):
keybd_event(VK_HOME, 0, 0, 0);
keybd_event(VK_HOME, 0, KEYEVENTF_KEYUP, 0);
to the OnGetEditMask event, but this event appears to be called multiple times when starting editing and when completing editing. Thankfully this did not work, as I would have been tempted to leave it in...
I've attempted to trace back through the parent classes to determine exactly where the select all is being called but got lost tracing through asm code in the system unit. I read you can add a D- compiler directive to a unit to have the debugger skip over those units, but I'm really hesitant about modifying any of the VCL units for obvious reasons.
Looking for any solution at this point (modify sql query? live binding property? field property?) but ideally would like to know exactly where this happens so that I can store the initial value of the cell at the same time. (I want to add the normal spreadsheet-type behavior of escape-while-editing restores initial value).
It looks like the class chain I need to follow is TInplaceEdit
-> TCustomMaskEdit
-> TCustomEdit
-> TWinControl
I assume it's in there somewhere.