DisplayLabel on Field is ignored until table is not open

Hi,

There is problem with using DisplayLabel for fields in TDAMemDataTable. DiplayLabel is not used in grid until table is opened first time. If you close table later then DisplayLabel stays.

Replication is easy:

  • add TDBGrid, TDAMemDataTable and TDADataSource to form and link them.
  • Add Field with different name and DisplayLabel
  • Activate data table, Create New field in grid, Deactivate data table. (correct label in design-time)
  • On run-time label is field name until table is opened.

See attached example.

Hi,

looks like you forgot to attach example.

but I can explain this behavior: standard Delphi DB controls like DBGrid work with TDataset descendant only. TDAMemDataTable isn’t a TDataset descendant but we have internal TDataset (i.e. TDAMemDataTable.Dataset) that is used as source for DB controls.

fields in internal TDataset are created in run-time at opening of table.
in your case, DBGrid shows field name when table isn’t opened, i.e. fields aren’t created in internal dataset

workaround: assign Title.Caption in TDBGrid column editor, in this case, it will work correctly.

GridLabels.zip (5.9 MB)

I understand that your component works other way than the most others. On other hand this cause troubles. In reality we don’t use regular Delphi grid but DevExpress one. I used regular one in example to eliminate third-party.

Your suggested workaround is not practical for many reasons and completely invalidates any use for schema/dataset display labels . Reason for use display labels is to be consistent across application. Create every single grid column manually will be time consuming and prone to mistakes.

Workflow is following:

  • Activate dataset or Get design-time data
  • Import fields into grid
  • Deactivate dataset
  1. UI does not allow change caption on field immediately because it’s already what you want and therefore not saved.
  2. If you manage to save it (Close/Open project, Change caption, Save) then if you activate dataset again (eg. to import to other grid) then display label is matched with column caption and after save is lost.

That mean you must check all labels after every dataset activation even you have not touch grid.

Hi,

add this code to your app and it will update captions automatically:

procedure TForm1.FormCreate(Sender: TObject);
begin
  UpdateCaptions(DBGrid2);
end;

procedure TForm1.UpdateCaptions(dbgrid: TDBGrid);
var
  i: integer;
begin
  if DBGrid.DataSource is TDADataSource then begin
    for i := 0 to dbgrid.Columns.Count -1 do
      dbgrid.Columns[i].Title.Caption := TDADataSource(DBGrid.DataSource).DataTable.FieldByName(dbgrid.Columns[i].FieldName).DisplayLabel;
  end;
end;