Forum > Databases

<SOLVED>How to make DBGrid show data type and length?


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,


--- Quote from: Jonvy on June 15, 2022, 10:20:41 am ---As the attachment, in postgres Admin, it has this function.
--- End quote ---
What makes you think, it's a DBGrid PostGres-Admin is using?

--- Quote ---How can I do, so my program can get same effect?
--- End quote ---
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

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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TMyGrid = type(TDBGrid);...  for i := 0 to DBGrid1.Columns.Count-1 do begin    F := DBGrid1.Columns[i].Field;    s := GetEnumName(TypeInfo(TFieldType), integer(F.DataType));  // requires unit TypInfo in "uses"    Delete(s, 1, 2);    if (F is TStringField) then s := s + '(' + IntToStr(F.Size) + ')';    DBGrid1.Columns[i].Title.Caption := DBGrid1.Columns[i].Title.Caption + LineEnding + '[' + s + ']';    DBGrid1.Columns[i].Title.MultiLine := true;    DBGrid1.Columns[i].Title.Alignment := taCenter;  end;  TMyGrid(DBGrid1).RowHeights[0] := 2*DBGrid1.DefaultRowHeight;
Find a tested sample project in the attachment

Thanks wp, it solves my problem.


[0] Message Index

Go to full version