Recent

Author Topic: Truncate long strings with ellipsis for displaying in DBGrid  (Read 2118 times)

maurobio

  • Hero Member
  • *****
  • Posts: 640
  • Ecology is everything.
    • GitHub
Dear ALL,

In a DBGrid I have a column (field) which stores short nucleotide sequences that, however, are long enough to make the column appear quite large, eg:

GAATAACAGCAATGGCAAAGTTCTTACTCCTCCTATCTGCCTTCGCAGTCCTCCTCCTGGTGGCTAACGCCTCCATTTACCGAGCCATTGTGGAGGTTGAAGAAGACTCGGGCCGTGAGCAGAGTTGCCAACGGCAGTTC

Is there a way of truncating this string with ellipsis (but just for displaying in the DBGrid, not in the database itself)?

Also, could it be possible to have a button (with an ellipsis icon) that, on being clicked, would show a simple dialog window containing a memo component with the contents of the sequence field?

Thanks in advance!

With best regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 3.8 - FPC 3.2.2 on GNU/Linux Mint 19.1/20.3, Windows XP SP3, Windows 7 Professional, Windows 10 Home

dsiders

  • Hero Member
  • *****
  • Posts: 1464
Re: Truncate long strings with ellipsis for displaying in DBGrid
« Reply #1 on: June 27, 2023, 03:20:04 am »
Dear ALL,

In a DBGrid I have a column (field) which stores short nucleotide sequences that, however, are long enough to make the column appear quite large, eg:

GAATAACAGCAATGGCAAAGTTCTTACTCCTCCTATCTGCCTTCGCAGTCCTCCTCCTGGTGGCTAACGCCTCCATTTACCGAGCCATTGTGGAGGTTGAAGAAGACTCGGGCCGTGAGCAGAGTTGCCAACGGCAGTTC

Is there a way of truncating this string with ellipsis (but just for displaying in the DBGrid, not in the database itself)?

Also, could it be possible to have a button (with an ellipsis icon) that, on being clicked, would show a simple dialog window containing a memo component with the contents of the sequence field?

Thanks in advance!

With best regards,

Any column on the grid can display an Ellipsis when its button style is set to cbsEllipsis. You can also set its Width, or set MinSize, and MaxSize for the column to control auto-fill sizes when enabled.
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

maurobio

  • Hero Member
  • *****
  • Posts: 640
  • Ecology is everything.
    • GitHub
Re: Truncate long strings with ellipsis for displaying in DBGrid
« Reply #2 on: June 27, 2023, 12:53:07 pm »
Hi, @dsiders!

Quote
Any column on the grid can display an Ellipsis when its button style is set to cbsEllipsis. You can also set its Width, or set MinSize, and MaxSize for the column to control auto-fill sizes when enabled.

I cannot remember right now why is that I had not seen one of these options before! But I will take a look at them!

Thanks a lot!

With best regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 3.8 - FPC 3.2.2 on GNU/Linux Mint 19.1/20.3, Windows XP SP3, Windows 7 Professional, Windows 10 Home

wp

  • Hero Member
  • *****
  • Posts: 13210
Re: Truncate long strings with ellipsis for displaying in DBGrid
« Reply #3 on: June 27, 2023, 05:05:34 pm »
Any column on the grid can display an Ellipsis when its button style is set to cbsEllipsis. You can also set its Width, or set MinSize, and MaxSize for the column to control auto-fill sizes when enabled.
I would not "abuse" the cbsEllipsis Buttonstyle for displaying an ellipsis for long lines because the ButtonStyle is meant to assign dedicated cell editors to columns. DBGrid inherits most of the Options introduced in the basic TCustomGrid, among them there is dgCellEllipsis. So, when you activate this option too-long lines will end with '...', but the editing behaviour is not changed.

maurobio

  • Hero Member
  • *****
  • Posts: 640
  • Ecology is everything.
    • GitHub
Re: Truncate long strings with ellipsis for displaying in DBGrid
« Reply #4 on: June 28, 2023, 01:42:11 pm »
Hi, @wp!

Quote
I would not "abuse" the cbsEllipsis Buttonstyle for displaying an ellipsis for long lines because the ButtonStyle is meant to assign dedicated cell editors to columns.

I do understand, but in my case the DBGrid is read-only and therefore no editing is possible anyway. By clicking on the ellipsis button, the user will be presented with a dialog box where she/he can peruse the entire content of the long string field (in my case, the short nucleotide sequence).

I will post a test here ASAP.

With warmest regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 3.8 - FPC 3.2.2 on GNU/Linux Mint 19.1/20.3, Windows XP SP3, Windows 7 Professional, Windows 10 Home

maurobio

  • Hero Member
  • *****
  • Posts: 640
  • Ecology is everything.
    • GitHub
Re: Truncate long strings with ellipsis for displaying in DBGrid
« Reply #5 on: June 29, 2023, 01:54:44 am »
Hi,

Here is my first test attempt at using a TDBGrid with long strings truncated for display.

I could not devise how to correctly configure the column with the long strings (the last one, "SEQUENCE", in the test data spreadsheet) to add ellipsis for displaying its contents.

Notice that the grid dataset is a TWorkSheetDataset from wp's fSpreadsheet package.

Thanks for any assistance you can provide.

With warmest regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 3.8 - FPC 3.2.2 on GNU/Linux Mint 19.1/20.3, Windows XP SP3, Windows 7 Professional, Windows 10 Home

paweld

  • Hero Member
  • *****
  • Posts: 1494
Re: Truncate long strings with ellipsis for displaying in DBGrid
« Reply #6 on: June 29, 2023, 07:00:27 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   i: Integer;
  4.   col: TColumn;
  5. begin
  6.   sWorksheetDataset1.FileName := 'TestData.xls';
  7.   sWorksheetDataset1.Open;
  8.   for i := 0 to sWorksheetDataset1.Fields.Count - 1 do
  9.   begin
  10.     col := DBGrid1.Columns.Add;
  11.     col.Field := sWorksheetDataset1.Fields[i];
  12.     if col.Width > 300 then
  13.     begin
  14.       col.ButtonStyle := cbsEllipsis;
  15.       col.Width := 300;
  16.     end;
  17.   end;
  18. end;  
Best regards / Pozdrawiam
paweld

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Truncate long strings with ellipsis for displaying in DBGrid
« Reply #7 on: June 29, 2023, 07:44:20 am »
The width of the column needs to be smaller that the text, in your example its not. If you shrink the column you will see the 3 dots.

maurobio

  • Hero Member
  • *****
  • Posts: 640
  • Ecology is everything.
    • GitHub
Re: Truncate long strings with ellipsis for displaying in DBGrid
« Reply #8 on: June 29, 2023, 04:37:17 pm »
Hi, @paweld, @eljo!

Thanks for your replies. paweld code contributed to somewhat clarify the things to me, but it is not yet what I am willing to achieve. In fact, I want the ellipsis to appear only in the column 'SEQUENCE'; furthermore, I need to make the ellipsis respond to a mouse click, so that a dialog box can be displayed showing the full contents of the field.

I vaguely remember to have seen, many years ago, a DBGrid control for Delphi which showed a button with an ellipsis [...] besides a long string in a column of the grid, but now I am not sure which control was that.

Can something like that be achieved with Lazarus TDBGrid control?

With best regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 3.8 - FPC 3.2.2 on GNU/Linux Mint 19.1/20.3, Windows XP SP3, Windows 7 Professional, Windows 10 Home

maurobio

  • Hero Member
  • *****
  • Posts: 640
  • Ecology is everything.
    • GitHub
Re: Truncate long strings with ellipsis for displaying in DBGrid
« Reply #9 on: June 30, 2023, 02:21:28 am »
Dear ALL,

I just made it! Here is my solution:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   i: integer;
  4.   col: TColumn;
  5. begin
  6.   sWorksheetDataset1.FileName := 'TestData.xls';
  7.   sWorksheetDataset1.Open;
  8.   for i := 0 to sWorksheetDataset1.Fields.Count - 1 do
  9.   begin
  10.     col := DBGrid1.Columns.Add;
  11.     col.Field := sWorksheetDataset1.Fields[i];
  12.     if col.Width > 300 then
  13.       col.Width := 300;
  14.   end;
  15. end;
  16.  

The procedure TForm.FormCreate above (slightly modified from the code provided by paweld) displays ellipsis only for the long string field (the SEQUENCE column).

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
  2. var
  3.    FieldStr, AValue: AnsiString;
  4. begin
  5.   if Key = VK_RETURN then
  6.   begin
  7.     FieldStr := sWorksheetDataset1.FieldDefs[DBGrid1.SelectedIndex].Name;
  8.     if FieldStr = 'SEQUENCE' then
  9.     begin
  10.       AValue := sWorksheetDataset1.Fields[DBGrid1.SelectedIndex].AsString;
  11.       ShowMessage(AValue);
  12.     end;
  13.   end;
  14. end;
  15.  

In the procedure TForm.DBGrid1KeyDown above, the contents of the field SEQUENCE are displayed when the ENTER key is pressed on that column.

With warmest regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 3.8 - FPC 3.2.2 on GNU/Linux Mint 19.1/20.3, Windows XP SP3, Windows 7 Professional, Windows 10 Home

 

TinyPortal © 2005-2018