55
TMS SOFTWARE
TADVSTRINGGRID
DEVELOPERS GUIDE
62 | Page
TAdvStringGrid cell and cell properties access
Various properties enable handling cell data. The most simple way is to use the
grid.Cells[ACol,ARow]: string property. In addition TAdvStringGrid provides:
grid.AllCells[ACol,ARow]: string;
Access the grid cell as string irrespective of hidden
columns or rows. grid.AllCells returns the cell as
displayed, ie. after possible processing of the real cell
text by the event OnGetDisplText
grid.AllFloats[ACol,ARow]: Double;
Access the grid cell as float irrespective of hidden
columns or rows
grid.AllGridCells[ACol,ARow]: string;
Access the grid cell as string irrespective of hidden
columns or rows. grid.AllGridCells returns the cell as
stored, ie. before possible processing by the event
OnGetDisplText
grid.AllObjects[ACol,ARow]: TObject;
Access the TObject that can be associated with each
cell irrespective of hidden columns or rows
grid.AllWideCells[ACol,ARow]: widestring Access the grid cell as widestring irrespective of
hidden columns or rows
grid.Dates[ACol,ARow]: TDateTime;
Access the grid cell as date
grid.Floats[ACol,ARow]: Double;
Access the grid cell as double. If no floating point data
is in the cell, the value 0.0 is returned. When setting
the cell data through grid.Floats, the grid.FloatFormat
property is used to format the floating point data as
text.
grid.GridCells[ACol,ARow]: string;
Access the grid cell as string. grid.GridCells returns the
cell as stored, ie. before possible processing by the
event OnGetDisplText
grid.Ints[ACol,ARow]: Integer;
Access the grid cell as integer. If no integer is in the
cell, the value 0 is returned.
grid.Objects[ACol,ARow]: TObject;
Access the TObject that can be associated with each
cell
grid.Times[ACol,ARow]: TDateTime;
Access the grid cell as time
grid.WideCells[ACol,ARow]: widestring
Access the grid cell as widestring
grid.OriginalCells[ACol,ARow]: string
Provides access to cells irrespective of column
ordering. The cells can be accessed with the original
column index before a user started to move columns
(when goColMoving is true in grid.Options)
grid.GetSelectionAsText: string
Returns all text in one or multiple selected cells as
text. The text format is compatible with how Excel
40
TMS SOFTWARE
TADVSTRINGGRID
DEVELOPERS GUIDE
63 | Page
puts multiple cell content on the clipboard.
Grid.SetSelectionAsText(Value:string)
Sets the contents of the selected cells with text that is
formatted in the same way that Excel formats multiple
cell text on the clipboard.
Two ways exist to apply colors, fonts & alignment to grid cells. A dynamic way exists that allows
setting these properties through events. The dynamic cell settings through events is a flexible and
memory friendly way to apply colors, alignment etc.. to grid cells as no additional storage is
required per cell for storing these cell properties.
Dynamic cell properties
The events to handle these settings are:
OnGetCellColor:
TGridColorEvent = procedure(Sender: TObject; ARow, ACol: Integer;
AState: TGridDrawState; ABrush: TBrush; AFont: TFont ) of object;
This event is triggered when painting a cell and queries for the background brush of the cell and the
font.
OnGetAlignment:
TGridAlignEvent = procedure (Sender: TObject; ARow, ACol: Integer;
var HAlign: TAlignment;var VAlign: TAsgVAlignment) of object;
The grid align event is also triggered when painting a cell and queries for horizontal and vertical
text alignment in a cell. Note that the default grid alignment is set via the property
grid.DefaultAlignment. When grid.AutoNumAlign is set to true, the grid will automatically right-
justify cells that contain numbers.
OnGetCellGradient:
TGridGradientEvent = procedure(Sender: TObject; ARow, ACol: Integer;
var Color, ColorTo, ColorMirror, ColorMirrorTo: TColor) of object;
This event is triggered to dynamically set a dual (mirrored) gradient. The upper half rectangular
gradient is from Color to ColorTo, the bottom half rectangular gradient is from ColorMirror to
ColorMirrorTo.
Example: setting font color and alignment depending on cell values
procedure TForm1.AdvStringGrid1GetCellColor(Sender: TObject; ARow,
ACol: Integer; AState: TGridDrawState; ABrush: TBrush; AFont: TFont);
begin
if grid.Ints[ACol,ARow] > 0 then
AFont.Color := clBlack
else
How to C#: Basic SDK Concept of XDoc.PDF for .NET XDoc.PDF for .NET allows C# developers to edit hyperlink of PDF document, including editing PDF url links and quick navigation link in bookmark/outline.
chrome pdf from link; add links to pdf in preview VB.NET PDF: Basic SDK Concept of XDoc.PDF XDoc.PDF for .NET allows VB.NET developers to edit hyperlink of PDF document, including editing PDF url links and quick navigation link in bookmark/outline.
adding links to pdf in preview; add a link to a pdf
34
TMS SOFTWARE
TADVSTRINGGRID
DEVELOPERS GUIDE
64 | Page
AFont.Color := clRed;
end;
procedure TForm1.AdvStringGrid1GetAlignment(Sender: TObject; ARow,
ACol: Integer; var HAlign: TAlignment; var VAlign: TVAlignment);
begin
if (grid.Ints[ACol,ARow] >= 1000) then
HAlign := taRightJustify
else
HAlign := taLeftJustify;
end;
Static cell properties
Cell properties can also be set directly. Using this approach of course requires more memory as the
properties are stored with each cell. Possible properties are:
property Alignments[Col,Row: Integer]: TAlignment;
property Colors[Col,Row: Integer]: TColor;
property ColorsTo[Col,Row: Integer]: TColor;
property FontColors[Col,Row: Integer]: TColor;
property FontStyles[Col,Row: Integer]: TFontStyles;
property FontSizes[Col,Row: Integer]: Integer;
property FontNames[Col,Row: Integer]: string;
Example: setting a cell 2,3 to red background, bold Tahoma font and right aligned
Grid.Colors[2,3] := clRed;
Grid.FontStyles[2,3] := Grid.FontStyles[2,3] + [fsBold];
Grid.FontNames[2,3] := ‘Tahoma’;
Grid.Alignments[2,3] := taRightJustify;
Note: the property grid.ColorsTo[Col,Row: Integer]: TColor is used for specifying vertical gradients
in cells from color set by Colors[] to color set by ColorsTo[].
This sets a vertical gradient from red to white in cell 1,1:
Grid.Colors[1,1] := clRed;
Grid.ColorsTo[1,1] := clWhite;
28
TMS SOFTWARE
TADVSTRINGGRID
DEVELOPERS GUIDE
65 | Page
TAdvStringGrid row & column methods
TAdvStringGrid provides several methods to access, insert, remove, move, swap rows and columns.
Grid.InsertRows(RowIndex, RCount: integer);
Inserts RCount rows in the grid at position RowIndex
Grid.RemoveRows(RowIndex, RCount: integer);
Deletes RCount rows in the grid at position RowIndex
Grid.MoveRow(FromIndex, ToIndex: integer);
Moves one row at position FromIndex to the new positon ToIndex
Grid.MoveRows(FromIndex, ToIndex, RCount: integer);
Moves RCount rows starting from index FromIndex to ToIndex
Grid.SwapRows(Row1, Row2: integer);
Swaps rows Row1 and Row2
Grid.AddRow;
Adds one row to the grid
Grid.InsertCols(ColIndex, CCount: integer);
Inserts CCount rows in the grid at position ColIndex
Grid.RemoveCols(ColIndex, CCount: integer);
Deletes CCount columns in the grid at position ColIndex
Grid.MoveColumn(FromIndex, ToIndex: integer);
Moves one column at positon FromIndex to the new position ToIndex
Grid.SwapColumns(Col1, Col2: integer);
Swaps columns Col1 and Col2
Grid.DistinctValues(Col: integer): TStrings;
Returns a stringlist of distinct values in column Col.
65
TMS SOFTWARE
TADVSTRINGGRID
DEVELOPERS GUIDE
66 | Page
TAdvStringGrid cell graphics
TAdvStringGrid has support to add all kinds of graphics to a cell. These include:
Bitmap
Windows bitmap
Icon
Windows icon
ImageList
Imagelist element
DataImage
Cell data dependent imagelist element
Images
Multiple imagelist elements
Picture
Picture
FilePicture
Picture file reference
Rotated
Rotated text
Comment
Comment indicator
CheckBox
Checkbox
DataCheckBox Cell data dependent checkbox
Radiogroup
Radiobuttons
Radiobutton
Radiobutton
XP Progress
XP style Progressbar
Progress
Progressbar
ProgressPie
Progress pie
RangeIndicator Bi-color range indicator
Button
Button
BitButton
BitButton
Balloon Balloon
Shapes
Series of graphic shapes
Rating
Rating control
Interface
Custom graphics via interface
Bitmaps
The functions available to handle bitmaps in cells are:
function CreateBitmap(ACol,ARow: Integer;transparent: Boolean;
hal:TCellHalign; val:TCellValign):TBitmap;
procedure AddBitmap(ACol,ARow: Integer;ABmp:TBitmap;Transparent: Boolean;
hal:TCellHalign; val:TCellValign);
procedure RemoveBitmap(ACol,ARow: Integer);
function HasBitmap(ACol,ARow: Integer): Boolean;
function GetBitmap(ACol,ARow: Integer): TBitmap;
The difference between CreateBitmap and AddBitmap is that with CreateBitmap, the bitmap
instance is created, maintained and destroyed by the grid while with AddBitmap it is the
responsibility of the programmer to create the instance and destroy it.
In code this difference becomes clear:
// add bitmap from resource to the grid
Grid.CreateBitmap(2,3,True,haBeforeText,vaTop).LoadFromResourceName(HInstan
ce,’TEST’);
36
TMS SOFTWARE
TADVSTRINGGRID
DEVELOPERS GUIDE
67 | Page
var
Bmp: TBitmap;
Bmp := TBitmap.Create;
Bmp.LoadFromResourceName(HInstance,’TEST’);
Grid.AddBitmap(2,3,True,haBeforeText,vaTop);
// at the end of the application, the bitmap needs to be destroyed
Bmp.Free;
Icons
The functions available to handle icons in cells are:
function CreateIcon(ACol,ARow: Integer; hal:TCellHalign;
val:TCellValign):TIcon;
procedure AddIcon(ACol,ARow: Integer;AIcon:TIcon; hal:TCellHalign;
val:TCellValign);
procedure RemoveIcon(ACol,ARow: Integer);
The same logic applies for Icons as for Bitmaps for the difference between CreateIcon and AddIcon.
Imagelist elements
An image from the imagelist assigned the the grid.GridImages property can be inserted in a cell.
The following methods are available for this:
procedure AddImageIdx(ACol,ARow,Aidx:
Integer;hal:TCellHalign;val:TCellValign);
procedure RemoveImageIdx(ACol,ARow: Integer);
function GetImageIdx(ACol,ARow: Integer;var idx: Integer): Boolean;
The Idx parameter is the index of the image in the imagelist. The GetImageIdx returns false if
GetImageIdx was called for a cell that does not contain an imagelist element.
It is also possible to add an imagelist element with an index that is set through the cell text with
these methods:
procedure AddDataImage(ACol,ARow,Aidx: Integer; hal:TCellHalign;
val:TCellValign);
procedure RemoveDataImage(ACol,ARow: Integer);
function HasDataImage(ACol,ARow: Integer): Boolean;
To set image 2 from the imagelist in a cell 2,3, this requires:
Grid.AddDataImage(2,3,2,haBeforeText,vaTop);
42
TMS SOFTWARE
TADVSTRINGGRID
DEVELOPERS GUIDE
68 | Page
This sets the cell text to ‘2’. If later the cell text is changed to ‘3’, the image will automatically
change to image 3 of the imagelist.
Multiple imagelist elements
To add multiple images in a cell, two methods are defined:
procedure AddMultiImage(ACol,ARow,Dir: Integer; hal:TCellHalign;
val:TCellValign);
procedure RemoveMultiImage(ACol,ARow: Integer);
The Dir parameter sets the direction of the images, with 0 = horizontal and 1 = vertical.
After calling AddMultiImage, the indexes of the images can be set with the property
Grid.CellImages[ACol,ARow]: TIntList;
Example: setting 3 imagelist based images in a cell
Grid.AddMultiImage(2,3,0,haBeforeText,vaTop);
Grid.CellImages[2,3].Add(2); // index of first image
Grid.CellImages[2,3].Add(0); // index of second image
Grid.CellImages[2,3].Add(5); // index of third image
Pictures
Adding pictures is very similar to adding bitmaps to a cell. The CreatePicture and AddPicture are
available to add a picture that is either created, maintained and destroyed by the grid or a picture
that is created, maintained and destroyed by the application. An extra parameter for adding
pictures is the stretch mode. This controls how the picture is stretched in the cell and can be:
TStretchMode =
(noStretch,Stretch,StretchWithAspectRatio,Shrink,ShrinkWithAspectRatio);
noStretch
the picture is not stretched
Stretch
stretch horizontally & vertically to fit in the cell
StretchWithAspectRatio
stretch horizontally & vertically with aspect ratio to fit in the cell
Shrink
only shrink the image when it is too large for the cell
ShrinkWithAspectRatio
shrink with aspect ratio when image is too large
function CreatePicture(ACol,ARow:Integer; transparent:Boolean;
stretchmode:TStretchMode; padding:Integer; hal:TCellHalign;
val:TCellValign):TPicture;
procedure AddPicture(ACol,ARow: Integer;APicture:TPicture;transparent:
Boolean; stretchmode:TStretchMode; padding: Integer; hal:TCellHalign;
val:TCellValign);
41
TMS SOFTWARE
TADVSTRINGGRID
DEVELOPERS GUIDE
69 | Page
procedure RemovePicture(ACol,ARow: Integer);
function HasPicture(ACol,ARow: Integer): Boolean;
function GetPicture(ACol,ARow: Integer): TPicture;
With normal pictures, once the pictures are created or added, the picture requires memory
necessary for holding the picture. When holding a large amount of large pictures, this can quickly
become a problem. Therefore, a TFilePicture can be created and inserted. A TFilePicture only
contains a reference to the file picture and does not require memory to hold the picture. The
TFilePicture will load and display the picture only for the visible cells.
function CreateFilePicture(ACol,ARow: Integer;Transparent: Boolean;
StretchMode:TStretchMode; padding:Integer; hal:TCellHalign;
val:TCellValign): TFilePicture;
procedure AddFilePicture(ACol,ARow:
Integer;AFilePicture:TFilePicture;Transparent:
Boolean;stretchmode:TStretchMode;padding:
Integer;hal:TCellHalign;val:TCellValign);
procedure RemoveFilePicture(ACol,ARow: Integer);
function GetFilePicture(ACol,ARow: Integer): TFilePicture;
Example: adding a picture with normal picture methods and file picture methods
Grid.CreatePicture(2,3,True,Shrink,0,haLeft,vaTop).LoadFromFile(‘TST.JPG’);
Grid.CreateFilePicture(2,3,True,Shrink,0,haLeft,vaTop).Filename :=
‘TST.JPG’;
Rotated text
Text rotated in any angle can be added in a cell. Note that it is required that font used for the cell
is a TrueType font. Non truetype fonts are not guaranteed to work with text rotation. Following
methods are available to help with handling rotated text in cells:
procedure AddRotated(ACol,ARow: Integer; AAngle: Smallint; s: string);
procedure SetRotated(ACol,ARow: Integer; AAngle: SmallInt);
procedure RemoveRotated(ACol,ARow: Integer);
function IsRotated(ACol,ARow: Integer;var aAngle: Integer): Boolean;
Adding 90 degrees rotated text is as such easy:
Grid.AddRotated(2,3,90,’This is rotated’);
Comments
A comment indicator is a little triangle in the right top corner of the cell that indicates a comment
text is available for the cell. When the mouse is over the comment indicator, this comment is
displayed as a hint. The color of the little triangle comment indicator is red by default but can be
set in another color with the property grid.ControlLook.CommentColor: TColor or it can be set as
parameter of the method AddColorComment.
40
TMS SOFTWARE
TADVSTRINGGRID
DEVELOPERS GUIDE
70 | Page
This is an overview of comment related methods & properties:
procedure AddComment(ACol,ARow: Integer; Comment:string);
Adds a comment with default color to cell ACol,ARow.
procedure AddColorComment(ACol,ARow: Integer; Comment:string; Color:
TColor);
Adds a comment with color to cell ACol,ARow.
procedure RemoveComment(ACol,ARow: Integer);
Removes the comment from cell ACol,ARow.
procedure RemoveAllComments;
Removes comments from all cells in the grid.
function IsComment(ACol,ARow: Integer;var comment:string): Boolean;
The IsComment method returns true when the specified cell effectively contains a comment and it
returns this comment text in the comment parameter.
property CellComment[ACol,ARow: integer]: string;
Provides access to cell comments as property.
Checkbox and DataCheckbox
Three types of checkboxes exist. A normal checkbox can be added to a cell with some text. The
checkbox state is set through the SetCheckBoxState method. A normal checkbox can have two
states: checked and not checked. When checked, its state is true, when unchecked, its state is
false. The second type checkbox is a tri-state checkbox. Notice the overrides for the methods
AddCheckBox, GetCheckBoxState, SetCheckBoxState. When used with Boolean parameters, this
applies to normal two state checkboxes. When used with the TCheckBoxState parameter, this
applies to tri-state checkboxes that can have the states: cbChecked, cbUnchecked, cbGrayed. A
third checkbox type is the data checkbox. A data checkbox is added to a cell and the checkbox
state reflects the cell text. If the cell text is equal to the grid.CheckTrue property, the checkbox is
displayed as checked, if the cell text is equal to the grid.CheckFalse property, the checkbox is
displayed as not checked. The checkbox is displayed grayed when the cell is set to readonly with
the OnCanEditCell event. If it is not desirable that a checkbox looks disabled for readonly cells, set
grid.NoDisabledCheckRadioLook = true. If a data checkbox is used, clicking the checkbox will cause
the cell text to change from grid.CheckFalse to grid.CheckTrue or vice versa.
This is an overview of methods that can be used with checkboxes:
procedure AddCheckBox(ACol,ARow: Integer;State,Data: Boolean); overload;
procedure AddCheckBox(ACol,ARow: State: TCheckBoxState); overload;
procedure RemoveCheckBox(ACol,ARow: Integer);
function HasCheckBox(ACol,ARow: Integer): Boolean;
function HasDataCheckBox(ACol,ARow: Integer): Boolean;
Documents you may be interested
Documents you may be interested