I recently discovered how to do that without making complicated code.
There are an event that you can change to do that and nothing more.
It is not a question it is just a solution because I see some people what can find this useful. And I want to contribute here because I like Lazarus and I'm a Pascal veteran (LOL) programmer coming from the past years from Delphi and Kylix. I think that Lazarus is the best choice to stay in.
Ok, here my contribution:
- Place a TDBGrid in your form (If you don't have already one placed).
Just modify it as you want (configure properties and so as you wish). - Now add an OnPrepareCanvas event. It can be done easily at design time (go to Object Inspector choose Events tab, double click in OnPrepareCanvas).
- And use a code like this:
procedure TForm1.DBGrid1PrepareCanvas(sender: TObject; DataCol: Integer;
Column: TColumn; AState: TGridDrawState);
var
MyTextStyle: TTextStyle;
begin
// Here you choose what column will be affected (the columns of the DBGrid not SQL).
if (DataCol = 1) then
begin
// The next is not neccesary but you can use it to adjust your text appearance.
// you can change colors, font, size, as well other parameters.
MyTextStyle := TDBGrid(Sender).Canvas.TextStyle;
MyTextStyle.SingleLine := False;
MyTextStyle.Wordbreak := False;
TDBGrid(Sender).Canvas.TextStyle := MyTextStyle;
// Here how to show any text:
// just assign an event procedure to OnGetText of the Field.
Column.Field.OnGetText := @Form1.DBGridOnGetText;
end;
end;
- Well, if you try to compile, the last code will raise an error because we need to add the procedure as follows:
- In the form class add the procedure declaration:
procedure DBGridOnGetText(Sender: TField; var aText: string; DisplayText: Boolean);
(It is in the beginning where all procedures and controls are defined for the form)
- Now add the code for the procedure:
procedure TForm1.DBGridOnGetText(Sender: TField; var aText: string; DisplayText: Boolean);
begin
if (DisplayText) then
aText := Sender.AsString;
end;
As you can see it will show all as String (the code talks for himself).
So watch out what kind of content you try to show.
Make a better code if you want to be selective about what kind of content to show and how to show it. Anyway you are limited to showing strings only, but you can show some Boolean values like 'True' and 'False' for better representation. Too you can show any number as crude String or you can format it before. You can format dates as you like too and so on. Imagination is your limit.
I recommend you to make some condition to detect if there are a text like as show in the memo and proceed, otherwise take the default behaviour. But if you want to format something like dates and so on, please use the already included functions (dateutils) because that are more faster and secure than try to process all data char by char in code.
My code is so simple because I never will get the situation where the data is not a memo (TEXT field in DataBase). That is because I choose it when I check "DataCol = 1", so I'm sure that this event only will be executed when the data comes from the DataCol number 1 (What I have assigned as the TEXT field in my DataBase).
Well, I hope this can be useful for someone.
I was really searching for something like this for a long time but never takes the time to do it for myself because the suggestions what I found in Internet are related with drawing in canvas for myself the text, but as this example you don't need to draw anything, only assign the content as string to the variable. That is easy and is just what I was searching for.