Recent

Author Topic: <SOLVED>How to make DBGrid show data type and length?  (Read 797 times)

Jonvy

  • Jr. Member
  • **
  • Posts: 90
<SOLVED>How to make DBGrid show data type and length?
« on: June 15, 2022, 10:20:41 am »
Dear all,
I want to display table's fied type and data length together with table name in DBGrid in my program.

As the attachment, in postgres Admin, it has this function.

How can I do, so my program can get same effect?

Best regards,
Jonvy
« Last Edit: June 21, 2022, 10:47:38 am by Jonvy »

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: How to make DBGrid show data type and length?
« Reply #1 on: June 15, 2022, 11:08:38 am »
As the attachment, in postgres Admin, it has this function.
What makes you think, it's a DBGrid PostGres-Admin is using?

Quote
How can I do, so my program can get same effect?
You don't with DBGrid

EDIT: Hold on.
Just got an idea....
You need two FixedRows
and your underlying SQL has to Ship the Field-Types as the first record
IIRC, the first fixed row gets the Field-Names, everything else is the returned data
« Last Edit: June 15, 2022, 11:11:11 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

wp

  • Hero Member
  • *****
  • Posts: 11912
Re: How to make DBGrid show data type and length?
« Reply #2 on: June 15, 2022, 01:03:13 pm »
The DBGrid column header can be changed at runtime. For any column (index i), get the required information from the associated field and add it to the current title after a LineBreak. When you set the column's header MultiLine to true the header will be displayed in two lines. You also need to double the height of the title row for which you need to cast TDBGrid to a descendent type in order to access the protected RowHeights property.

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyGrid = type(TDBGrid);
  3. ...
  4.   for i := 0 to DBGrid1.Columns.Count-1 do begin
  5.     F := DBGrid1.Columns[i].Field;
  6.     s := GetEnumName(TypeInfo(TFieldType), integer(F.DataType));  // requires unit TypInfo in "uses"
  7.     Delete(s, 1, 2);
  8.     if (F is TStringField) then s := s + '(' + IntToStr(F.Size) + ')';
  9.     DBGrid1.Columns[i].Title.Caption := DBGrid1.Columns[i].Title.Caption + LineEnding + '[' + s + ']';
  10.     DBGrid1.Columns[i].Title.MultiLine := true;
  11.     DBGrid1.Columns[i].Title.Alignment := taCenter;
  12.   end;
  13.   TMyGrid(DBGrid1).RowHeights[0] := 2*DBGrid1.DefaultRowHeight;

Find a tested sample project in the attachment

Jonvy

  • Jr. Member
  • **
  • Posts: 90
Re: How to make DBGrid show data type and length?
« Reply #3 on: June 15, 2022, 04:16:30 pm »
Thanks wp, it solves my problem.
« Last Edit: June 21, 2022, 10:47:14 am by Jonvy »

 

TinyPortal © 2005-2018