delphidbgridfdmemtable

How do you right justify DBGrid titles when using a FDMemtable in Delphi?


I can't seem to get the fixed row Titles in a DBGrid to align right justified when using a FDMemtable. Whenever I set the Field alignment to taRightJustify it right justifies the data cells perfectly. However, the DBGrid titles are always left justified.

What's even more frustrating is I can set the corresponding DBGrid column title alignment to taRightJustify and it appears perfectly fine in the IDE. But when I run the program the column title shows as left justified.

Has anyone found a way to make DBGrid column titles stay right justified when using a FDMemtable?

BTW, this also happens with taCenter. The data cells align centered but the titles stay left justified.

PEBKAC

The issue was of my own making. I did not invoke the DBGrid Columns Editor and add all the fields. Instead, I was using the "Structure" pane and getting to the DBGrid columns that way. Although the Structure pane allowed me to modify the column titles this was only temporary and did not persist when the program was run.


Solution

  • I can't reproduce the issue using a TFDMemTable either.

    I dropped a TFDMemTable, TDataSource, and TDBGrid on a new VCL application's main form, connected them as usual (the grid's datasource set to DataSource1, the datasource's DataSet set to FDMemTable1), and then added the below code to the form's OnCreate event:

    procedure TForm3.FormCreate(Sender: TObject);
    begin
      FDMemTable1.FieldDefs.Add('ID', ftInteger, 0, True);
      FDMemTable1.FieldDefs.Add('LastName', ftString, 20);
      FDMemTable1.FieldDefs.Add('FirstName', ftString, 20);
      FDMemTable1.FieldDefs.Add('Salary', ftCurrency);
      FDMemTable1.CreateDataSet;
      FDMemTable1.Active := True;
      FDMemTable1.AppendRecord([1, 'Smith', 'John', 30000]);
      FDMemTable1.AppendRecord([2, 'Jones', 'Jane', 40000]);
      FDMemTable1.AppendRecord([3, 'Doe', 'David', 2500]);
      DBGrid1.Columns[3].Alignment := TAlignment.taRightJustify;
      DBGrid1.Columns[3].Title.Alignment := TAlignment.taRightJustify;
    end;
    

    It also works correctly if I set everything up at designtime. Repeat the same setup steps I used above, but instead of using the code, use the following steps:

    1. Select FDMemTable1 in the Object Inspector. At the bottom of the OI, click the LoadFromFile link, and navigate to the BDS Samples data folder (by default, in C:\Users\Public\Public Documents\Embarcadero\Studio\17.0\Samples\Data) and select animals.fds. (No specific reason for choosing that one, except it has a numeric field we can use for testing.)

    2. Right-click on the DBGrid, and choose Columns Editor, or click the ellipsis button on the DBGrid.Columns property in the Object Inspector. Right-click in the Columns Editor and choose Add all fields.

    3. Select either the Size or Weight column, expand it's Title property, and set Alignment to taRightJustify.

    4. Run the application. The column you modified in step #3 above has a right-aligned title. (Here I used the Size column.)

    Grid with Size column title right-aligned