Documents
NextGrid & NextDBGrid
- Integrating Inplace Editors
- Upgrade existing projects for loading changes
- FieldChooser Tutorial
- NextGrid How To
- Html Column Tutorial
- NextGrid Slide Style Tutorial
- InputLine Tutorial
- Edit events in NextGrid
- NextGrid Vista Style
NextGrid
- NextGrid Quick Start
- Graphic Column Tutorial
- Optimize NextGrid
- Custom Draw in NextGrid
- VirtualColumn Tutorial
- NextGrid Custom Sorting
- TreeColumn Tutorial
NextDBGrid
NextInspector
- NextInspector Item Types
- NextInspector Quick Start
- NextDBInspector Tutorial
- NextInspector Advanced tutorial
- Map VCL property to item
NextSheet
NextCollection
- NxInfoPanel Tutorial
- NxPathControl and NxNotebook tutorial
- NxAlertWindow Tutorial
- NxPathControl Quick Start
- NxButton Tutorial
- NxOutlookBar Tutorial
- NxFlipPanel and NxHeaderPanel Quick Start
- NxPageControl and NxNotebook Quick Start
Misc
- DateTime FormatMask
- NxAutoCompletion Tutorial
- Using NxColorScheme
- NxComboBox styles
- Enable typing unicode characters in InplaceEditors
- NxProgress Tutorial
- NxPreview Quick Start
- Numeric FormatMask
- Component Names Change
- 32bit Bitmaps Tutorial
Labs
VirtualColumn Tutorial
Related Articles:
Info
- Skill:
- Version: 1.0.0
- Author: Wim van der Vegt
- Created:
- Updated: 2008-07-28
Downloads
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.