-1

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

MyGridColumn.HeaderAlignmentVert := TcxAlignmentVert.vaCenter;

but this not work :(

zeus
  • 12,173
  • 9
  • 63
  • 184
  • 1
    "Does not work" is too vague - what were you expecting? – Brian Feb 19 '20 at 23:00
  • @Brian I mean after doing MyGridColumn.HeaderAlignmentVert := TcxAlignmentVert.vaCenter; the alignment is still on the top :( – zeus Feb 19 '20 at 23:06
  • 1
    Show in an image - it works fine here. Set header height to 100, set HeaderAlignmentVert to Center for one of the field columns and the column caption is centered vertically in the header. – Brian Feb 19 '20 at 23:09
  • 1
    For future reference, *this not work* is an absolutely useless problem description unless you explain exactly what you expected to happen and clearly explain what it did instead. IOW, explain *how specifically* it doesn't work, so we have some information to go on. If you don't understand why, call your auto repair shop and say *My car isn't working. xIt's a Ford, and it's blue. What's wrong with it?* with no other details, and see how that works. – Ken White Feb 20 '20 at 01:35
  • Have you tried setting this property at design time? My guess is that once the grid is rendered, and you set at run time, the grid will need repainting.. – John Easley Feb 20 '20 at 02:02
  • 1
    It would allso help if you provided a [mcve] that shows where exactly you're using that singleline of code, because the location of that code is relevant. You've been here long enough to know how this site works, I'd think. – Ken White Feb 20 '20 at 03:46
  • @JohnEasley : I just add an answer with the solution to my problem :) it's because I did OptionsView.HeaderEndEllipsis = True – zeus Feb 20 '20 at 19:18
  • @Brian like i say to John : I just add an answer with the solution to my problem :) it's because I did OptionsView.HeaderEndEllipsis = True – zeus Feb 20 '20 at 19:19
  • @KenWhite : I just add an answer with the solution to my problem :) it's because I did OptionsView.HeaderEndEllipsis = True – zeus Feb 20 '20 at 19:20

2 Answers2

2

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.

MartynA
  • 30,454
  • 4
  • 32
  • 73
  • I don't know why someone gave -1 to your answer doesn't matter. yes exactly something in my project is overriding the alignement :( maybe the style or something like this ? – zeus Feb 20 '20 at 14:41
  • Thanks, I don't know why it got a downvote either. Anyway, if you you my code exactly as it is. do you see the difference in vertical positions of the 3 header texts? Then, comment out the line which sets the HeadeHeight to 100: can you see the differences in vertical position after that? – MartynA Feb 20 '20 at 17:17
1

Ok I found the problem It's because i set

OptionsView.HeaderEndEllipsis = True

When you do this then the

MyGridColumn.HeaderAlignmentVert := TcxAlignmentVert.vaCenter;

is not working :(

zeus
  • 12,173
  • 9
  • 63
  • 184
  • I get that behaviour with the example in my answer and the existing project I mentioned. – MartynA Feb 20 '20 at 19:47
  • Developer Express is pretty good with feature requests / bug reports for things like this. Reproduced as well with the currently latest version 19.2.4. Would be nicer if it respected alignment until it was full and then put in the ellipsis. – Brian Feb 20 '20 at 20:39