Jump to content


Photo

Guid Column


  • Please log in to reply
4 replies to this topic

#1 wvd_vegt

wvd_vegt

    Master Member

  • Honorable Members
  • PipPipPipPipPip
  • 710 posts
  • Gender:Male
  • Location:the Netherlands

Posted 31 October 2005 - 03:04 PM

Hi all

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);


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.


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

G.W. van der Vegt

#2 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 31 October 2005 - 03:22 PM

Thank you Wim again,

I will see to add it.

But, I have one problem that "Columns Pallete" is bigger and bigger and maybe I can add some Page Control to have similar organization of Components like in Delphi bellow version 8
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#3 wvd_vegt

wvd_vegt

    Master Member

  • Honorable Members
  • PipPipPipPipPip
  • 710 posts
  • Gender:Male
  • Location:the Netherlands

Posted 31 October 2005 - 10:28 PM

Hi, Boki,

Sounds like a good idea the pagecontrol. But you need to make the editor slightly bigger for it to fit. Btw hows work on the treecolumn?
G.W. van der Vegt

#4 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 31 October 2005 - 10:41 PM

Hello Wim,

I will made one lite version of my NxPageControl and I will put it inside Column Editor.

I think that I will include TreeColumn in v3.4

Best regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#5 wvd_vegt

wvd_vegt

    Master Member

  • Honorable Members
  • PipPipPipPipPip
  • 710 posts
  • Gender:Male
  • Location:the Netherlands

Posted 23 October 2006 - 05:51 PM

Hi Boki,

Is it possible to add this column type to the next release now there is more space available in the ColumnEditor?
G.W. van der Vegt




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users