Recent

Author Topic: Showing Memos in DBGrid (and all kind of text)  (Read 14705 times)

anyeos

  • New Member
  • *
  • Posts: 14
Showing Memos in DBGrid (and all kind of text)
« on: December 26, 2012, 08:55:08 am »
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:
Code: [Select]
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:
Code: [Select]
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:
Code: [Select]
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.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Showing Memos in DBGrid (and all kind of text)
« Reply #1 on: December 26, 2012, 09:54:29 am »
Thanks for your contribution!

FYI, there's this page where this kind of information can be found:
http://wiki.lazarus.freepascal.org/Grids_Reference_Page
...and which could be updated if it doesn't contain your nicely explained snippet ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

anyeos

  • New Member
  • *
  • Posts: 14
Re: Showing Memos in DBGrid (and all kind of text)
« Reply #2 on: December 26, 2012, 05:34:59 pm »
Yes, that page just helped me a lot with this.

Thank you for that. I guess the world need more people like you. You did wrote a lot of useful information and for free! That is a big work! guy!

I don't see in that page this solution as I exposed here. Maybe you include it in that. Because you already have a nice sorting and uniform document in that page :)

I forgot to add:
Code: [Select]
MyTextStyle.Layout := tlTop;
In the code of the OnPrepareCanvas procedure. If you don't, then you will get a sliced text (center aligned vertically) if there are NewLine characters in the text.


BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Showing Memos in DBGrid (and all kind of text)
« Reply #3 on: December 27, 2012, 09:06:26 am »
Yes, that page just helped me a lot with this.

Thank you for that. I guess the world need more people like you. You did wrote a lot of useful information and for free! That is a big work! guy!
If you look at the history of that page you will see it was a lot of people. I just helped...

I don't see in that page this solution as I exposed here. Maybe you include it in that. Because you already have a nice sorting and uniform document in that page :)
... and I suggest YOU add information to it, not me, to help others: you know and tested the solution, I haven't. Also, I'm busy with other things to improve FPC/Lazarus.
If more people help, the work gets lighter. If everything gets left to a few, not much gets done.


Good luck.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

anyeos

  • New Member
  • *
  • Posts: 14
Re: Showing Memos in DBGrid (and all kind of text)
« Reply #4 on: December 29, 2012, 05:28:10 am »
Sorry, but I'm very busy right now. And I don't have a habit to edit a wiki (I respect the work of other people so I only do that if I know I can do a quality work). And for that I need more time.
I used my time to publish here because here are more people movement. So, someone can just do that.

Hey, don't underestimate your work. I know there are other people contributing but you are here the most of the time.
Excuse me but it appears that you are unhappy. Do you think that is there something wrong with my attitude?

goodname

  • Sr. Member
  • ****
  • Posts: 297
Re: Showing Memos in DBGrid (and all kind of text)
« Reply #5 on: December 29, 2012, 06:17:37 am »
From what I can tell the work posted here is of high enough quality for the wiki. Likely just want to remove some of the introduction and do a copy and paste.

Don't think that BigChimp is unhappy with your contribution. He just wants it put into the wiki by the person that did the work. That way the information is more likely to be correct and the right person gets the credit. If your initial post had been copied and pasted into the wiki it would have been incorrect as your update post shows.

It is likely that information left on the forum only will eventually become lost to all but the best searchers.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Showing Memos in DBGrid (and all kind of text)
« Reply #6 on: January 04, 2013, 05:08:05 pm »
Exactly, what Goodname said.

Thanks!
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

totya

  • Hero Member
  • *****
  • Posts: 643
Re: Showing Memos in DBGrid (and all kind of text)
« Reply #7 on: December 24, 2015, 06:33:46 pm »
I recently discovered how to do that without making complicated code.
(...)

Hi!

Thanks for this code! This is much better than this: link, because with your method the SQL Text data is untouched; for example, if I use DBMemo, I see the full length of text. With linked method, this data is truncated.

universe

  • New Member
  • *
  • Posts: 20
Re: Showing Memos in DBGrid (and all kind of text)
« Reply #8 on: March 27, 2016, 06:51:29 pm »
Hello, I'd like to thank you, your code helped me a lot and I made some changes to make it more versatile and portable, I changed Datacol with Column.Field.DataType
Code: Pascal  [Select][+][-]
  1.   if Column.Field.DataType in [ftMemo,ftWideMemo] then
  2.   begin
  3.     // The next is not neccesary but you can use it to adjust your text appearance.
  4.     // you can change colors, font, size, as well other parameters.
  5.     MyTextStyle := TDBGrid(Sender).Canvas.TextStyle;
  6.     MyTextStyle.SingleLine := False;
  7.     MyTextStyle.Wordbreak  := False;
  8.     MyTextStyle.Layout := tlTop;
  9.     TDBGrid(Sender).Canvas.TextStyle := MyTextStyle;
  10.  
  11.     // Here how to show any text:
  12.     // just assign an event procedure to OnGetText of the Field.
  13.     Column.Field.OnGetText := @DBGridOnGetText;  
  14.  

totya

  • Hero Member
  • *****
  • Posts: 643
Re: Showing Memos in DBGrid (and all kind of text)
« Reply #9 on: October 01, 2016, 12:39:45 pm »
Hello, I'd like to thank you, your code helped me a lot and I made some changes to make it more versatile and portable, I changed Datacol with Column.Field.DataType

Hi!

Thank you for this clean code! :)

vrull

  • Full Member
  • ***
  • Posts: 118
Re: Showing Memos in DBGrid (and all kind of text)
« Reply #10 on: November 09, 2016, 03:06:45 am »
In this piece of code
Quote
// Here how to show any text:
    // just assign an event procedure to OnGetText of the Field.
    Column.Field.OnGetText := @DBGridOnGetText;
you assign the same event handler to the same field as many times as many records are in your dataset.
Would not it be better to do it just once in advance, like
Code: Pascal  [Select][+][-]
  1.   for i := 0 to Pred(Source.FieldCount) do
  2.     if Source.Fields[i].DataType in [ftMemo,ftWideMemo] then
  3.       Source.Fields[i].OnGetText := @DBGridOnGetText;
  4.  

The solution itself is just perfect, I have just tested it and it works like a charm.
Thank you for sharing!
where Source is the dataset feeding the DBGrid?


 

TinyPortal © 2005-2018