If possible, how to add RadiobuttonGroup with texts in a TMSFNCGrid cell

Hi TMS-Support,

I am using TMSFNCGrid for a task. I have to create rows in a grid with field and value pairs dynamically from code. I have to show a text field, a combo box field, a radio button group in this grid. I can do it successfully for a simple text field and combo box field.

But I didn't find a way to display a radio button group in a grid cell. Adding a simple radio button is possible. Is this feature possible with TMSFNCGrid? Could you throw some light on this topic? Thank you.

With best regards,
Michael Neubert.

Hi,

This can be done via custom editing.

TMSFNCGrid1.DefaultRowHeight := 100;

procedure TForm1.TMSFNCGrid1CellEditGetData(Sender: TObject; ACol,
  ARow: Integer; CellEditor: TWinControl; var CellString: string);
var
  i: integer;
begin
  if TryStrToInt(CellString, i) then
  begin
    (CellEditor as TRadioGroup).Parent := TMSFNCGrid1;
    (CellEditor as TRadioGroup).ItemIndex := i;
    (CellEditor as TRadioGroup).Parent := nil;
  end;
end;

procedure TForm1.TMSFNCGrid1CellEditSetData(Sender: TObject; ACol,
  ARow: Integer; CellEditor: TWinControl; var CellString: string);
begin
  CellString := (CellEditor as TRadioGroup).ItemIndex.ToString;
end;

procedure TForm1.TMSFNCGrid1GetCellEditorCustomClassType(Sender: TObject;
  ACol, ARow: Integer; var CellEditorCustomClassType: TTMSFNCGridEditorClass);
begin
  CellEditorCustomClassType := TRadioGroup;
end;

procedure TForm1.TMSFNCGrid1GetCellEditorProperties(Sender: TObject; ACol,
  ARow: Integer; CellEditor: TWinControl);
begin
  (CellEditor as TRadioGroup).Parent := TMSFNCGrid1;
  (CellEditor as TRadioGroup).Items.Add('Item 1');
  (CellEditor as TRadioGroup).Items.Add('Item 2');
  (CellEditor as TRadioGroup).Items.Add('Item 3');
  (CellEditor as TRadioGroup).Items.Add('Item 4');
  (CellEditor as TRadioGroup).Parent := nil;
end;

procedure TForm1.TMSFNCGrid1GetCellEditorType(Sender: TObject; ACol,
  ARow: Integer; var CellEditorType: TTMSFNCGridEditorType);
begin
  CellEditorType := etCustom;
end;

image

Hi Pieter,

Thank you for the reply. I have been looking into your answer and grid events. I have still some questions:

In my scenario, I have to load dynamically text fields, checklistbox/radiogroup, and combo box onto the Grid cells with data from the database in specific cases. I can deal with text and combo box fields in a Grid.

Could you tell in which order should I have to use the events? I have to load a grid cell with texts in TChecklistbox and set its index and later when the index is changed I have to save it. Also looks like I have to use GetCellData, GetCellproperties, etc, at first rather than edit events.

Also after changing the index of a Tchecklistbox, maybe I need to call celleditGetData. What do you say? I am a bit puzzled. I hope it is clear what I meant. Thank you.

P.s : CellEditor class types with respect to CellEditGetData event (TWincontrol, TTMSFNCGridEditor) is contradicting the TMS FNC Grid Developer Guide and your above solution.

With best regards,
Sorpetaler Fensterbau GmbH.

Hi,

The above sample shows how to implement custom inplace editors. The events are called automatically when the grid cell is edited. If you want to manually map this on a dataset, you will need to call the appropriate field and update the value.

The value needs to be read from the database in the OnGetCellEditGetData and needs to be written to the database in the OnCellEditSetData. For example you would do something like this:

procedure TForm1.TMSFNCGrid1CellEditSetData(Sender: TObject; ACol,
  ARow: Integer; CellEditor: TWinControl; var CellString: string);
begin
  if CellEditor is TRadioGroup then
  begin
    CellString := (CellEditor as TRadioGroup).ItemIndex.ToString;
    ClientDataSet1.FieldByName('MyField').AsString := CellString;
  end;
end;

You would also need to set the correct record from the OnSelectCell event. You can take a look at the TTMSFNCGridDatabaseAdapter component to link it to a dataset automatically so you don't need to call the fields manually.