Documents

NextGrid & NextDBGrid

NextGrid

NextDBGrid

NextInspector

NextSheet

NextCollection

Misc

Labs

NextGrid .NET

VirtualColumn Tutorial

Related Articles:

Info
  • Skill:
  • Version: 1.0.0
  • Author: Wim van der Vegt
  • Created:
  • Updated: 2008-07-28
Downloads
From version 3.x NextGrid comes with a new column type TNxVirtualColumn. This column does not store any data but uses an event handler to retrieve its data.

The VirtualColumn Demo Program has a TNextGrid with a TNxTextualColumn and a TNxVirtualColumn. The demo program is very basic and uses the virtual column to display the length of the text in the textual column.

The OnGetText event handler for the TNxVirtualColumn looks like:

procedure TForm1.LengthColGetText(Sender: TObject; const ACol,
  ARow: Integer; var Value: WideString);
begin
  with TNextGrid(TNxVirtualColumn(Sender).GridComponent) do
    if (ARow >= 0) and (ARow < RowCount) then
      Value := IntToStr(Length(CellByName['TextCol', ARow].AsString));
end;


It simply sets the Value to be displayed to the length of the first column. Note that addressing cells by name and index is used and some checks are made that the cells referenced actually exists.

Next we add a button to add a row to the grid and add some text to its first column.

procedure TForm1.AddBtnClick(Sender: TObject);
begin
  with NextGrid1 do
    begin
      AddRow;
      CellByName['TextCol', 'Last'].AsString := 'The Quick Brown Fox...';
    end;
  TruncateBtn.Enabled := True;
end;


Note that after pressing this "Add" Button the second column is automatically updated as well and the code above is executed.



But when you edit the first column the second column isn't updated as the grid can't possibly know the data is updated (because the grid doesn't know where program gets the data from, it also doesn't know that the data is linked to the data of the first textual column).

To correct this behavior we need to tell the grid to update the virtual column as well whenever the first column is changed. The best spot is the OnAfterEdit handler of the first column as that's where the input for the data of the virtual column originates.

So if we add the following event handler code:

procedure TForm1.NextGrid1AfterEdit(Sender: TObject; ACol, ARow: Integer;
  Value: WideString);
begin
  with TNextGrid(Sender) do
    Columns.Column['LengthCol'].Refresh(gaBody);
end;


The grid will update the virtual column whenever the first column (of which the length is displayed in the virtual column) is changed after the used is finished with editing it.



The grid will update the virtual column whenever the first column (of which the length is displayed in the virtual column) is changed after the used is finished with editing it.

A last feature of the TNxVirtualColumn is the OnSetText event handler. It is fired whenever the text of the virtual column is set by the program. When for instance the following OnSetText event handler code is added, and the truncate button is pressed to set the value of the virtual columns first row to 10, the text of the textual column is truncated to 10 characters.

Was This Article Useful?

Only constructive comments, code contributions... will be publishes. Questions, non-official discussion will not be published.