FNCDataGrid with TButton as Custom Control

Hello,
I'm using FNCDataGrid with TButton as Custom Control in last column (Column 7). I'm adding Buttons in such way:

 for I := 1 to FNCDatGrid.RowCount - 1 do
 begin
   BT := TButton.Create(Self);
   BT.Caption := 'Info';
   FNCDatGrid.Controls[7, I] := BT
 end;

After calling a new select in database linked to FNCDataGridDatabaseAdapter my FNCDatGrid is changing, numbers of rows are changing. FNCDatGrid appear without TButtons, I'm creating and adding TButtons again. But TButtons created first are still in memory. Should I freeing them? Maybe there is another way to deal with such situations? I would be grateful for any hint.

Hi,

If you create a TButton with the Owner being Self, Self will clean up those instances when you destroy that instance, if Self is the main form, the form will be responsible. if you want more control over this, and let the grid manage and cleanup instances whenever you are refreshing cells, you need to use the following code instead:

 for I := 1 to FNCDatGrid.RowCount - 1 do
 begin
   BT := TButton.Create(nil);
   BT.Caption := 'Info';
   FNCDatGrid.Controls[7, I] := BT
   FNCDatGrid.ManagedObjects[7, I] := BT;
 end;

the Owner is nil, meaning you are responsible for destroying the object, and ManagedObjects keeps a reference, which will be destroyed when the cell is destroyed.

You can inspect potential memory leaks with

ReportMemoryLeaksOnShutDown := True in the formcreate of your application.

Alternatively, you can also configure a column as being a button column instead of manually adding buttons for each row, you can set the property Type to gcitButton

  TMSFNCDataGrid1.Columns[0].&Type := gcitButton;
  TMSFNCDataGrid1.Columns[0].AddSetting(gcsType);

Great, it's working perfectly. Thanks a lot

I'm using ctButton ColumnType in FNCGrid, and I also testing gcitButton in FNCDataGrid. But in this cases I can't attach Popup Menu or Image to Buttons. That's why FNCDataGrid with custom controls is a great solution for me.

And that's fine, there are multiple solutions for the same problem. Thanks for the feedback :slight_smile: