delphidevexpresstcxgriddevexpress-gridcontrol

how can I change the header alignment of my cxGrid table view?


How can I change the header alignment of my cxGrid tableView? I try

MyGridColumn.HeaderAlignmentVert := TcxAlignmentVert.vaCenter;

but this not work :(


Solution

  • The code below shows how to create a cxGrid, add a TcxGridDBTableView, and set the header alignments for the TableView's headers, all entirely in code. I've done it all in code because there are so many properties in the Object Inspector for a cxGrid and its contents that it's hard to see at a glance (in the OI or the DFM) what the relevant properties are.

    type
      TForm1 = class(TForm)
        CDS1: TClientDataSet;
        DS1: TDataSource;
        cxGrid1DBTableView1: TcxGridDBTableView;
        cxGrid1Level1: TcxGridLevel;
        cxGrid1: TcxGrid;
        procedure FormCreate(Sender: TObject);
      [...]
      public
        cxGrid : TcxGrid;
        cxLevel : TcxGridLevel;
        cxView : TcxGridDBTableView;
      end;
    [...]
    procedure TForm1.FormCreate(Sender: TObject);
    var
      Field : TField;
    begin
      // First, set up TClientDataSet
    
      Field := TIntegerField.Create(Self);
      Field.FieldKind := fkData;
      Field.FieldName := 'ID';
      Field.DataSet := CDS1;
    
      Field := TStringField.Create(Self);
      Field.FieldKind := fkData;
      Field.FieldName := 'Name';
      Field.Size := 40;
      Field.DataSet := CDS1;
    
      Field := TIntegerField.Create(Self);
      Field.FieldKind := fkData;
      Field.FieldName := 'Value';
      Field.DataSet := CDS1;
    
      CDS1.CreateDataSet;
    
      CDS1.IndexFieldNames := 'ID';
    
      CDS1.InsertRecord([1, 'One', 1]);
      CDS1.InsertRecord([1, 'Two', 2]);
      CDS1.InsertRecord([1, 'Three', 3]);
    
      CDS1.First;
    
      //  Next, create cxGrid, and add cxGridDBTableView
      cxGrid := TcxGrid.Create(Self);
      cxGrid.Parent := Self;
      cxGrid.Width := 400;
    
      cxLevel := cxGrid.Levels.Add;
      cxLevel.Name := 'Firstlevel';
    
      cxView := cxGrid.CreateView(TcxGridDBTableView) as TcxGridDBTableView;
      cxView.Name := 'ATableView';
      cxView.OptionsView.HeaderHeight := 100; // so we can easily see different vert alignments
    
      cxView.DataController.Options := cxView.DataController.Options + [dcoImmediatePost];
      cxView.DataController.KeyFieldNames := 'ID';
    
      cxLevel.GridView := cxView;
    
      cxView.DataController.DataSource := DS1;
      cxView.DataController.CreateAllItems;
    
      //  by this point the columns and their headers will have been created
    
      cxView.Columns[0].HeaderAlignmentVert := cxclasses.vaTop;
      cxView.Columns[1].HeaderAlignmentVert := cxclasses.vaCenter;
      cxView.Columns[2].HeaderAlignmentVert := cxclasses.vaBottom;
    end;
    

    For me, the headers have the correct alignments, ie

    ID
        Name
               Value
    

    Fwiw, before creating the above example, I tried setting the headers' vertical alignments on an existing project, and couldn't get it to work either, by which I mean, the headers texts all obstinately stayed in the vertical center. So I guess there must be some other setting in my existing project's grid which overrides the alignment behaviour.

    Update I noticed that if I comment out the line which sets the HeaderHeight, the vertical alignment setting appears to have no effect. I think that the reason is simply that the difference in vertical position of the text caused by different alignments is too small to notice - it is only after I increase the HeaderHeight to 23 or more that there is a visible difference.