Here a new type of column (for Boki) to integrate called a TGuidColumn. It's purpose is simple, when a rows is added a unique guid is created and displayed. This guid can be used to identify stuff linked to row that isn't show in the table.
For example in my current app I have a NxGridView above a rich text editor so i can edited multiple fragments of rtf. To save and load it i need a unique identifier to link the files to the table. The autonumber column is often to auto and a numeric column has problems when you delete & add rows. So i started using a normal string column filled with a guid to do the job, which resulted in the new column type.
In apps it will (have to) be invisible most of the time I think. Editing is also not that usefull as is trying to access it with any other methods than AsGuid and AsString.
The Guid is automatically created but because you can save & load the values like any other cell you end up with a nice unique identifier as column value.
Adding a column:
CODE
NextGrid1.Columns.Add(TNxGuidColumn, 'Guid');
Changing the value:
CODE
CoCreateGuid(g);
NextGrid1.Cell[1, 0].AsString := GuidToString(g);
NextGrid1.Cell[1, 0].AsString := GuidToString(g);
Using the AsGuid property:
CODE
ShowMessage(GuidToString((NextGrid1.Cell[1, 0] as TGuidCell).AsGuid));
Code:
CODE
{
Next Grid
Copyright (C) 1996-2005 by Berg
All rights reserved.
$id:NxGuidColumn.pas wim
}
unit NxGuidColumn;
interface
uses
ActiveX, Classes, NxEdit, NxColumns, NxColumnClasses, NxDisplays, NxCells;
type
TNxGuidColumn = class;
TGuidCell = class(TCell)
private
FValue: TGUID;
protected
function GetAsBoolean: Boolean; override;
function GetAsDateTime: TDateTime; override;
function GetAsFloat: Double; override;
function GetAsInteger: Integer; override;
function GetAsString: WideString; override;
function GetAsGuid: TGuid;
procedure SetAsBoolean(const Value: Boolean); override;
procedure SetAsDateTime(const Value: TDateTime); override;
procedure SetAsFloat(const Value: Double); override;
procedure SetAsInteger(const Value: Integer); override;
procedure SetAsString(const Value: WideString); override;
procedure SetAsGuid(const Value: TGuid);
public
constructor Create(Cells: TCells); override;
published
property AsGuid : TGUID read GetAsGuid write SetAsGuid;
end;
TNxGuidColumn = class(TNxCustomColumn)
private
FTextAfter: WideString;
FTextBefore: WideString;
FAutoExecute: Boolean;
procedure SetAutoExecute(const Value: Boolean);
procedure SetTextAfter(const Value: WideString);
procedure SetTextBefore(const Value: WideString);
protected
function GetColumnDisplayClass: TColumnDisplayClass; override;
public
procedure Assign(Source: TPersistent); override;
constructor Create(AOwner: TComponent); override;
function GetCellEditorClass: TCellEditorClass; override;
function IsKeyValid(Key: Char): Boolean; override;
published
property AutoExecute: Boolean read FAutoExecute write SetAutoExecute;
property TextAfter: WideString read FTextAfter write SetTextAfter;
property TextBefore: WideString read FTextBefore write SetTextBefore;
end;
TNxGuidColumnDisplay = class(TColumnDisplay)
public
procedure Paint; override;
end;
implementation
uses
NxGrid, SysUtils, Dialogs;
{ TNxGuidColumn }
procedure TNxGuidColumn.Assign(Source: TPersistent);
begin
inherited;
if Source is TNxGuidColumn then
begin
AutoExecute := TNxGuidColumn(Source).AutoExecute;
TextAfter := TNxGuidColumn(Source).TextAfter;
TextBefore := TNxGuidColumn(Source).TextBefore;
end;
end;
constructor TNxGuidColumn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAutoExecute := False;
FTextAfter := '';
FTextBefore := '';
DefaultValue := '';
Options := Options + [coShowTextFitHint];
SetSortType(stAlphabetic);
SetColumnType(ctGuid);
end;
procedure TNxGuidColumn.SetAutoExecute(const Value: Boolean);
begin
FAutoExecute := Value;
end;
procedure TNxGuidColumn.SetTextAfter(const Value: WideString);
begin
FTextAfter := Value;
end;
procedure TNxGuidColumn.SetTextBefore(const Value: WideString);
begin
FTextBefore := Value;
end;
function TNxGuidColumn.GetColumnDisplayClass: TColumnDisplayClass;
begin
Result := TNxGuidColumnDisplay;
end;
function TNxGuidColumn.GetCellEditorClass: TCellEditorClass;
begin
Result := TNxEdit;
end;
function TNxGuidColumn.IsKeyValid(Key: Char): Boolean;
begin
Result := Ord(Key) > 32;
end;
{ TGuidCell }
constructor TGuidCell.Create(Cells: TCells);
begin
inherited Create(Cells);
CoCreateGuid(FValue);
end;
function TGuidCell.GetAsBoolean: Boolean;
begin
Result := StrToBool(GetAsString);
end;
function TGuidCell.GetAsDateTime: TDateTime;
begin
Result := StrToDateTime(GetAsString);
end;
function TGuidCell.GetAsFloat: Double;
begin
Result := StrToFloat(GetAsString);
end;
function TGuidCell.GetAsGuid: TGuid;
begin
Result:=FValue;
end;
function TGuidCell.GetAsInteger: Integer;
begin
Result := StrToInt(GetAsString);
end;
function TGuidCell.GetAsString: WideString;
begin
Result:=GuidToString(FValue);
end;
procedure TGuidCell.SetAsBoolean(const Value: Boolean);
begin
SetAsString(BoolToStr(Value, True));
inherited;
end;
procedure TGuidCell.SetAsDateTime(const Value: TDateTime);
begin
SetAsString(DateTimeToStr(Value));
inherited;
end;
procedure TGuidCell.SetAsFloat(const Value: Double);
begin
SetAsString(FloatToStr(Value));
inherited;
end;
procedure TGuidCell.SetAsInteger(const Value: Integer);
begin
SetAsString(IntToStr(Value));
inherited;
end;
procedure TGuidCell.SetAsString(const Value: WideString);
begin
FValue := StringToGuid(Value);
inherited;
end;
procedure TGuidCell.SetAsGuid(const Value: TGuid);
begin
FValue := Value;
end;
{ TNxGuidColumnDisplay }
procedure TNxGuidColumnDisplay.Paint;
begin
with Column as TNxGuidColumn do
DrawTextRect(TextBefore + AsString + TextAfter, GetTextRect);
end;
end.
Next Grid
Copyright (C) 1996-2005 by Berg
All rights reserved.
$id:NxGuidColumn.pas wim
}
unit NxGuidColumn;
interface
uses
ActiveX, Classes, NxEdit, NxColumns, NxColumnClasses, NxDisplays, NxCells;
type
TNxGuidColumn = class;
TGuidCell = class(TCell)
private
FValue: TGUID;
protected
function GetAsBoolean: Boolean; override;
function GetAsDateTime: TDateTime; override;
function GetAsFloat: Double; override;
function GetAsInteger: Integer; override;
function GetAsString: WideString; override;
function GetAsGuid: TGuid;
procedure SetAsBoolean(const Value: Boolean); override;
procedure SetAsDateTime(const Value: TDateTime); override;
procedure SetAsFloat(const Value: Double); override;
procedure SetAsInteger(const Value: Integer); override;
procedure SetAsString(const Value: WideString); override;
procedure SetAsGuid(const Value: TGuid);
public
constructor Create(Cells: TCells); override;
published
property AsGuid : TGUID read GetAsGuid write SetAsGuid;
end;
TNxGuidColumn = class(TNxCustomColumn)
private
FTextAfter: WideString;
FTextBefore: WideString;
FAutoExecute: Boolean;
procedure SetAutoExecute(const Value: Boolean);
procedure SetTextAfter(const Value: WideString);
procedure SetTextBefore(const Value: WideString);
protected
function GetColumnDisplayClass: TColumnDisplayClass; override;
public
procedure Assign(Source: TPersistent); override;
constructor Create(AOwner: TComponent); override;
function GetCellEditorClass: TCellEditorClass; override;
function IsKeyValid(Key: Char): Boolean; override;
published
property AutoExecute: Boolean read FAutoExecute write SetAutoExecute;
property TextAfter: WideString read FTextAfter write SetTextAfter;
property TextBefore: WideString read FTextBefore write SetTextBefore;
end;
TNxGuidColumnDisplay = class(TColumnDisplay)
public
procedure Paint; override;
end;
implementation
uses
NxGrid, SysUtils, Dialogs;
{ TNxGuidColumn }
procedure TNxGuidColumn.Assign(Source: TPersistent);
begin
inherited;
if Source is TNxGuidColumn then
begin
AutoExecute := TNxGuidColumn(Source).AutoExecute;
TextAfter := TNxGuidColumn(Source).TextAfter;
TextBefore := TNxGuidColumn(Source).TextBefore;
end;
end;
constructor TNxGuidColumn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAutoExecute := False;
FTextAfter := '';
FTextBefore := '';
DefaultValue := '';
Options := Options + [coShowTextFitHint];
SetSortType(stAlphabetic);
SetColumnType(ctGuid);
end;
procedure TNxGuidColumn.SetAutoExecute(const Value: Boolean);
begin
FAutoExecute := Value;
end;
procedure TNxGuidColumn.SetTextAfter(const Value: WideString);
begin
FTextAfter := Value;
end;
procedure TNxGuidColumn.SetTextBefore(const Value: WideString);
begin
FTextBefore := Value;
end;
function TNxGuidColumn.GetColumnDisplayClass: TColumnDisplayClass;
begin
Result := TNxGuidColumnDisplay;
end;
function TNxGuidColumn.GetCellEditorClass: TCellEditorClass;
begin
Result := TNxEdit;
end;
function TNxGuidColumn.IsKeyValid(Key: Char): Boolean;
begin
Result := Ord(Key) > 32;
end;
{ TGuidCell }
constructor TGuidCell.Create(Cells: TCells);
begin
inherited Create(Cells);
CoCreateGuid(FValue);
end;
function TGuidCell.GetAsBoolean: Boolean;
begin
Result := StrToBool(GetAsString);
end;
function TGuidCell.GetAsDateTime: TDateTime;
begin
Result := StrToDateTime(GetAsString);
end;
function TGuidCell.GetAsFloat: Double;
begin
Result := StrToFloat(GetAsString);
end;
function TGuidCell.GetAsGuid: TGuid;
begin
Result:=FValue;
end;
function TGuidCell.GetAsInteger: Integer;
begin
Result := StrToInt(GetAsString);
end;
function TGuidCell.GetAsString: WideString;
begin
Result:=GuidToString(FValue);
end;
procedure TGuidCell.SetAsBoolean(const Value: Boolean);
begin
SetAsString(BoolToStr(Value, True));
inherited;
end;
procedure TGuidCell.SetAsDateTime(const Value: TDateTime);
begin
SetAsString(DateTimeToStr(Value));
inherited;
end;
procedure TGuidCell.SetAsFloat(const Value: Double);
begin
SetAsString(FloatToStr(Value));
inherited;
end;
procedure TGuidCell.SetAsInteger(const Value: Integer);
begin
SetAsString(IntToStr(Value));
inherited;
end;
procedure TGuidCell.SetAsString(const Value: WideString);
begin
FValue := StringToGuid(Value);
inherited;
end;
procedure TGuidCell.SetAsGuid(const Value: TGuid);
begin
FValue := Value;
end;
{ TNxGuidColumnDisplay }
procedure TNxGuidColumnDisplay.Paint;
begin
with Column as TNxGuidColumn do
DrawTextRect(TextBefore + AsString + TextAfter, GetTextRect);
end;
end.
Integration into NextGrid (excluding the column editor as I had no time to look into it and the obvious uses clauses):
NxCells.Pas / addColumn add:
CODE
ctGuid: ACell := TGuidCell.Create(Self);
NxCells.Pas / addRow add:
CODE
ctGuid: ACell := TGuidCell.Create(Self);
NxColumnEditor.pas / GetColumnClass add:
CODE
if ColumnCaption = 'Guid Column' then Result := TNxGuidColumn;
NxColumnEditor.pas / GetColumnImageIndex add:
CODE
if ColumnClass = TNxGuidColumn then Result := 0;
NxColumns.pas / TColumnType add:
CODE
ctGuid
NxGrid.pas / DrawCellData add:
CODE
ctGuid: Display.AsString := Cell[ACol, ARow].AsString;
NxGridReg.pas / Register / RegisterNoIcon add:
CODE
TNxGuidColumn
NxGridReg.pas / Register / RegisterClasses add:
CODE
TNxGuidColumn