2

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.

  • Btw, in view of the answers you've had, have you checked whether the problem is specific to using an FDMemTable? If you have, what was the result? – MartynA Mar 26 '16 at 00:07

2 Answers2

3

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

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Setting DBGrid1.Columns[3].Title.Alignment := TAlignment.taRightJustify; does indeeed work, thank you. Do you know why using the Object Inspector to set the very same value doesn't work? – Michael Riley - AKA Gunny Mar 25 '16 at 23:23
  • @MichaelRiley-AKAGunny: Sorry, Mike. I haven't had a chance to look into that; I'll try and look at it this weekend. – Ken White Mar 25 '16 at 23:59
  • Override your form's Loaded method and call inherited in it. Put a breakpoint in it after the call to inherited and you can then see whether the Columns[].Title.Alignment has been set to taRightJustify. – MartynA Mar 26 '16 at 00:01
  • @MartynA I'd love to give that a try but I don't know how to do what you are suggesting. Can you send me a link to resource that describes what you are talking about. – Michael Riley - AKA Gunny Mar 26 '16 at 15:57
  • You just add "procedure Loaded; override;" to the declaration of your form, press Ctrl-Shift-C to generate its implementation. The IDE inserts a call to inherited in its body. Put a breakpoint on inherited, then when the breakpoint triggers, single-step past it and see what the alignment is. – MartynA Mar 26 '16 at 16:05
  • 1
    @MichaelRiley-AKAGunny: Just did a new test. Use the same setup as above, except remove the FieldDefs and AddRecord code. Instead, load the FDMemTable from file, using the Samples Animals.FDS file (from the C:\Users\Public\Documents\Embarcadero\Studio\17.0\Samples\Data folder). Add all columns, and then use the columns editor to change the alignment of the Size column's Title to taRightJustify in the Object Inspector and run the app. The title appears right-aligned, just as you'd expect. – Ken White Mar 26 '16 at 16:08
  • @KenWhite Thank you. I was not using the DBGrid "Columns Editor" to add all the fields and then use the columns editor again to change the the column title alignment. I was looking in the "Structure" pane, saw the DBGrid1.Columns, clicked on the column in the "Structure" pane and modified the Ttitle alignment that way. It makes sense now. When I view the form as text without adding the columns using the DBGrid columns editor they don't show up. After adding the fields using the DBGrid columns editor they do show up when I view the form as text. Thank you for following up. – Michael Riley - AKA Gunny Mar 26 '16 at 16:39
  • @MichaelRiley-AKAGunny: Added steps to demonstrate using the OI to set column info for others in the future. :-) – Ken White Mar 26 '16 at 17:02
2

The code below works for me in Seattle. I'm using a TClientDataSet rather than a TFDMemTable, but I can't see that that would make any difference.

If you have persistent columns defined on your DBGrid, you can also set a column's title alignment via the Object Inspector - use it to select the column, then expand its Title node and you can set the title alignment there.

procedure TForm1.CDS1AfterOpen(DataSet: TDataSet);
var
  i : Integer;
begin
  for i := 0 to DBGrid1.Columns.Count - 1 do
    DBGrid1.Columns[i].Title.Alignment := taRightJustify;
end;

Btw, if you think you're setting the alignment in the OI but it's getting ignored, see if you can find out why, as follows:

  • Make sure your form is saved, then right-click on it and select View as text. Then, in the IDE editor window, you can see whether the Alignment property is saved as you've specified in the OI. Use the editor context menu to return to viewing the form as a form.

  • Add an override of the form's Loaded method as I've described in a comment. With a breakpoint on the inherited in Loadeds body, you can inspect the Alignment value before and after inherited is called.

Why am I suggesting looking into Loaded? Well, it is called after a form is streamed in from the DFM and is the routine where the run-time system finishes setting up the form. Sometimes, admittedly very rarely, another component (usually a 3rd-party one) misbehaves and causes strange behaviour of the properties of other compononents.

MartynA
  • 30,454
  • 4
  • 32
  • 73