TMSFNCDataGrid add ButtonColumn not working with new row

When adding a button column to a grid it works on existing rows only.

After adding a new row there is no button…

Which code are you using? If you have used TMSFNCDataGrid1.AddButtonColumn, it only applies to the rowcount at the time of configuration. If you want to mark a column being a button column, regardless of the amount of rows, use this code:

TMSFNCDataGrid1.AddColumn;
TMSFNCDataGrid1.Columns[TMSFNCDataGrid1.ColumnCount -1].&Type := gcitButton;
TMSFNCDataGrid1.Columns[TMSFNCDataGrid1.ColumnCount -1].TypeRange := gcirNormal;
TMSFNCDataGrid1.Columns[TMSFNCDataGrid1.ColumnCount -1].AddSetting(gcsType);

works fine…

How do I get access to properties and methods of that buttons?

At column level there are Control* properties (which applies to all controls including buttons). If you want to a have a more detailed control, for each button individually, you can use the OnGetCellProperties, where you can access the control property for a cell, or check which cell type it is, type cast it and access the button directly.

procedure TForm13.TMSFNCDataGrid1GetCellProperties(Sender: TObject;
  ACell: TTMSFNCDataGridCell);
begin
  if ACell.IsButtonCell then
    ACell.AsButtonCell.Button.//...
end;

Hi Pieter,

that works fine for some properties…

I am still missing access to button background color.

Here a summary of my tests:

procedure TFormTestFNCGrid.GridGetCellProperties(Sender: TObject; ACell: TTMSFNCDataGridCell);
begin
if ACell.IsButtonCell then
begin
// ACell.AsButtonCell.Button.Align:=TAlign.alClient; // !! not working - cell flickers, button disappeared
// ACell.AsButtonCell.Button.Margins.SetControlBounds(2,2,2,2); // !! not working
// ACell.AsButtonCell.Button.Font.Color:=clRed; // !! not working - color stays black
// ACell.AsButtonCell.Button.Style:=TCustomButton.TButtonStyle.bsPushButton; // !! not working
// ACell.AsButtonCell.Button.Brush.Style:=bsSolid; // !! not working - color not changing
// ACell.AsButtonCell.Button.Brush.Color:=clRed; // !! not working - color not changing

ACell.AsButtonCell.Button.Caption:='Caption';                                 // working
ACell.AsButtonCell.Button.Enabled:=true;                                      // working
ACell.AsButtonCell.Button.Visible:=true;                                      // working
ACell.AsButtonCell.Button.Font.Style:=[fsBold,fsItalic];                      // working
ACell.AsButtonCell.Button.Font.Size:=8;                                       // working
ACell.AsButtonCell.Button.Images:=VirtualImageList;                           // working
ACell.AsButtonCell.Button.ImageIndex:=0;

ACell.AsButtonCell.Button.OnClick:=OnButtonClick;                             // working

ACell.ControlAlign:=TTMSFNCDataGridCellControlAlign.gcaClient;                // working

end;
end;

Please use the Control* properties as much as possible for alignment/position, the control is actually positioned on top of the cell, it’s not part of the cell. Also the background color when using VCL is not possible, since it’s a native real TButton.

That stuff is really complicate…

When button is clicked, how do I get the row number of the button?

There is a seperate button click event at cell level: OnCellButtonClick