Current Lazarus, FPC fixes 2.6
Asked this before but got no further, so trying again...
I've got a dbgrid with memo fields (from a DBF file). In many cases, those memo fields contain not that much text at all.
By default, I think a memo field shows up as (Memo) in a dbgrid, which is a bit... useless.
I would like to
- show the full text of the cells. If the text is too big, it can be truncated
- optionally: in the selected cell, show scrollbars that allow the user to view the entire cell text
Having read the grids wiki page (but without Delphi experience) I've cobbled together this code, which doesn't work - it doesn't seem to paint correctly:
procedure TForm1.ResultsGridDrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
// Draw memo text instead of (Memo)
// Note: grid DefaultDrawing property must be off to avoid duplicate drawing
// To get this, I basically copied the existing DefaultDrawColumnCell procedure
// but tested for memo first. If no memo, pass on to default procedure.
// Maybe slower, more complicated, but it allows for changes in the
// core Lazarus DefaultDrawColumnCell procedure.
var
OverrideDraw: boolean; //determine if we're going to override drawing
S: string;
F: TField;
DataRow: Integer;
begin
OverrideDraw:=false;
try
F := Column.Field;
if F.DataType = ftMemo then
begin
OverRideDraw:=true;
end;
except
on E: exception do
begin
// We might have an inactive datalink or whatever,
// in that case, pass on our problems to the existing
// procedure.
//showMessage('Exception: ' + E.Classname + '/' + E.Message);
OverRideDraw:=false;
end;
end;
if OverRideDraw=false then
begin
// Call normal procedure to handle drawing for us.
ResultsGrid.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end
else
begin
// Get to work displaying our memo contents
// Basically shamelessly ripped from
// DefaultDrawColumnCell
// maybe fix something for first/header row
if F<>nil then
begin
//DO display memo ;)
S := F.AsString; //DisplayText will only show (Memo)
end
else
begin
S := '';
end;
//Actual drawing, taken from Grids.DrawCellText coding:
ResultsGrid.Canvas.TextRect(Rect,Left,Top, S);
end;
end;
Help welcome.
Project source code can be found under the outputparser directory in
https://bitbucket.org/reiniero/db2securityscript/src (You can download the code as a zip, too)
Thanks!