Sign in Join
Wiki copytemporary

TECGrid: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Overview
22
33**TECGrid** ![](/wiki-images/8/8c/Ecgrid.png) is a lightweight grid component. It does not contain any strings, it has no [[TDataSource|DataSource]] connection and its is not possible to custom-draw each cell. Therefore it's not alternative to [[TStringGrid]], [[TDBGrid]] nor [[TDrawGrid]]. It is designed to show content of any data structure and all displayed strings are taken from OnGetHeaderText and OnGetDataCellText events. It features bi-directional mode, fixed columns, fixed rows, column moving, column sizing, various fixed-cell styles and many others, described below.
44
5-<figure>
6-<img src="tecgrid.png" title="tecgrid.png" />
7-<figcaption>tecgrid.png</figcaption>
8-</figure>
5+![](/wiki-images/1/19/Tecgrid.png)
96
107TECGrid is part of [[Eye-Candy Controls]] (shortly ECControls or EC-Controls) set of visual controls written for Lazarus. Their design is based on Themes, therefore its look is very native everywhere, no matter what widgetset you use.
118
129TECGrid is installed to the tab EC-C on the Lazarus component palette.
1310
1411Each release is announced on Lazarus Forum in section Third Party Announcements. There are always attached files `README.txt` (list of all known issues) and `CHANGELOG.txt` (list of all changes from previous release).
1512
1613### License
1714
1815GNU Lesser General Public License 2.0 with linking exception (a.k.a. Modified LGPL). File ectabctrl.pas contains license header. Also, files `COPYING.modifiedLGPL.txt` and `COPYING.LGPL.txt` are bundled to each archive.
1916
2017### Author
2118
2219This component is written by Blaazen. Copyright notice and real name is mentioned in the header of the unit. You can contact author on Lazarus Forum (nickname: Blaazen) in any thread about EC-Controls. If you are logged in to forum, you can get author's e-mail or send him private message.
2320
2421### Download and Install
2522
2623See [[Eye-Candy Controls|Eye-Candy Controls#Install]]
2724
2825## Options
2926
3027### Property Options
3128
3229| Option | Description |
3330|----|----|
3431| egoAlwaysShowEditor | Editor is shown always; Grid and Column must not be egoReadOnly/ecoReadOnly |
3532| egoColMoving | Columns can be moved by mouse; 1) Except fixed columns 2) Columns can be always moved by code |
3633| egoColSizing | Columns can be resized by mouse; Column must be ecoSizing |
3734| egoDottedGrid | Grid line is dotted |
3835| egoEnlargeAlways | Column is enlarged even if Editor is not opened |
3936| egoHeaderPushedLook | Column header has pushed look when clicked |
4037| egoHilightCol | Whole column is highlighted |
4138| egoHilightRow | Whole row is highlighted |
4239| egoHorizontalLines | Show horizontal lines of grid |
4340| egoReadOnly | Grid is ReadOnly, it never opens Editor |
4441| egoScrollKeepVisible | Selection remains visible while scrolling (scrollbars/mouse wheel) |
4542| egoSortArrow | Sorting arrow in title is visible; 1) Column must be ecoSorting 2) Grid must have FixedRows\>=1 |
4643| egoTabs | Tab key goes through cells instead of other components |
4744| egoUseOrder | Array OrderedCols is defined; flag excfOrder for Load/SaveColumnsToXML is available |
4845| egoVerticalLines | Show vertical lines of grid |
4946| egoWheelScrollsGrid | Mouse wheel scrolls the grid, selection does not change (i.e. it rolls away) |
5047
5148### Other Grid Properties
5249
5350| | |
5451|----|----|
5552| AlternateColor | Color of even data rows |
5653| AlternateTint | Tint of even data rows in percents, 0 = normal color, 100 = AlternateColor |
5754| Columns | Collection of columns |
5855| FixedCols | Number of fixed columns |
5956| FixedRowHeight | Height of fixed (header) rows |
6057| FixedRows | Number of fixed (header) rows |
6158| GridLineColor | Color of grid lines (data area only) |
6259| GridLineWidth | Width of grid lines (data area only) |
6360| Images | ImageList for column headers; see Column.Title.ImageIndex property |
6461| RowHeight | Height of data rows |
6562| Style | Style of cells of fixed rows and/or fixed columns |
6663
6764### Property Column.Option
6865
6966| Option | Description |
7067|----|----|
7168| ecoCellToHint | Content of data cell is shown as a Hint (tooltip) |
7269| ecoEnlargePixels | Property EnlargeWidth means pixels, otherwise means percents |
7370| ecoReadOnly | Column is ReadOnly (never shows editor) |
7471| ecoSizing | Column can be resized by mouse; Grid must be egoColSizing |
7572| ecoSorting | Sorting arrow in title is visible; Grid must be egoSortArrow and must have FixedRows\>=1 |
7673| ecoVisible | Column is visible |
7774
7875### Property Column.Title
7976
8077| Option | Description |
8178|----|----|
8279| Alignment | Text alignment of the column header; bi-directional mode is respected |
8380| FontOptions | Font differences of the column header to Grid.Font |
8481| ImageIndex | Image displayed in the column header; taken from Grid.Images |
8582| Text | Caption of the column header; the only text that is not taken from events |
8683
8784### Other Column Properties
8885
8986| Option | Description |
9087|----|----|
9188| Alignment | Text alignment of the column; bi-directional mode is respected |
9289| Color | Background color of the column |
9390| ColorTint | Tint of background color in percents, 0 = Grid.Color/Grid.AlternateColor, 100 = Column.Color |
9491| EnlargeWidth | Increment to the column width; value means pixels or percents, depend on option ecoEnlargePixels |
9592| FontOptions | Font differences to Grid.Font |
9693| MaxWidth | Maximal width of the resizable column |
9794| MinWidth | Minimal width of the resizable column |
9895
9996## Using the Component
10097
10198### Mouse
10299
103100Left button
104101
105102Click on a data-cell always selects it and possibly opens editor (depends on options egoAlwaysShowEditor, egoReadOnly, ecoReadOnly and implementation of OnSelectEditor event).
106103
107104Click on a cell of fixed columns changes row, which in turn changes selection and possibly opens editor (see above).
108105
109106Click on a cell of fixed rows changes SortIndex if option ecoSorting of Column is set. It happens even if option egoSortArrow is not set (arrow is invisibe).
110107
111108Middle button
112109
113110Does nothing.
114111
115112Right button
116113
117114Opens context menu. Note that Grid, Column and Column.Title can have different context menu.
118115
119116Dragging and Resizing
120117
121118Dragging is realized by left mouse button. Dragging moves column on the new position (egoColMoving must be set). Fixed columns cannot be moved. Data columns cannot be moved in front of fixed columns. Shape of the mouse cursor shows if the column can be moved to the new position or not.
122119
123120Resizing is realized by left mouse button. Mouse must be pushed on the right edge of the column header (or the left edge in right-to-left BiDiMode) - when mouse cursor changes its shape. Column width can be resized between MinWidth and MaxWidth. Both Options egoColSizing and ecoSizing must be set.
124121
125122Mouse Wheel
126123
127124Wheel moves selection up/down.
128125
129126Wheel + CTRL key (exactly ssModifier) scrolls grid.
130127
131128Option egoWheelScrollsGrid reverses this behaviour.
132129
133130### Keyboard
134131
135132(Grid must be focused).
136133
137134Arrow keys change selection.
138135
139136Arrow keys + CTRL key (exactly ssModifier) scroll grid.
140137
141138Home/End keys skip to the first/last data column.
142139
143140Home/End keys + CTRL key (exactly ssModifier) skip to the first/last data row.
144141
145142PageUp/PageDown keys skip up/down by the number of visible rows.
146143
147144PageUp/PageDown keys + CTRL key (exactly ssModifier) scroll up/down by the number of visible rows.
148145
149146If option egoTabs is set, Tab key skips to the next data cell. If selection reaches the last column, it skips to the first column and the next row. Shift + Tab skips in the opposite direction.
150147
151148Enter, Space, F2, BackSpace and Delete keys open Editor (Grid/Column must not be egoReadOnly/ecoReadOnly).
152149
153150Keys 0..9 and A..Z also open Editor.
154151
155152CTRL+C copies content of the selection to the clipboard.
156153
157154### Code
158155
159156`procedure BeginUpdate; override;`
160157
161158Reduces number of calculations and repaints when multiple properties change.
162159
163160`function CellRect(ACol, ARow: Integer): TRect;`
164161
165162Returns rectangle of the specified cell.
166163
167164`function CellRectEditor(ACol, ADataRow: Integer): TRect;`
168165
169166Returns corrected rectangle of the specified cell, it takes care of the GridLineWidth. Recommended for opening Editor.
170167
171168`procedure EndUpdate; override;`
172169
173170Each call of BeginUpdate; must be followed by EndUpdate. All changes will be processed.
174171
175172`function IsCellFullyVisible(ACol, ARow: Integer): Boolean;`
176173
177174Returns True if specified cell is fully visible.
178175
179176`function IsCellVisible(ACol, ARow: Integer): Boolean;`
180177
181178Returns True if specified cell is at least partially visible.
182179
183180`function IsColFullyVisible(ACol: Integer): Boolean;`
184181
185182Returns True if AColl is fully visible.
186183
187184`function IsColVisible(ACol: Integer): Boolean;`
188185
189186Returns True if ACol is at least partially visible.
190187
191188`function IsRowFullyVisible(ARow: Integer): Boolean;`
192189
193190Returns True if ARow is fully visible.
194191
195192`function IsRowVisible(ARow: Integer): Boolean;`
196193
197194Returns True if ARow is at least partially visible.
198195
199196`procedure LoadColumnsFromXML(AColumnsNode: TDOMNode; AXMLFlags: TXMLColFlags = cXMLColFlagsAll); overload;`\
200197`procedure LoadColumnsFromXML(AFileName: string; AColumnsNode: DOMString; AXMLFlags: TXMLColFlags = cXMLColFlagsAll); overload;`
201198
202199Loads parameters of columns. Option egoUseOrder must be set for using the Order properties.
203200
204201`function MakeCellFullyVisible(ACol, ARow: Integer; AForceColVisible: Boolean): Boolean;`
205202
206203Makes specified cell visible (without selecting it). If column is invisible, it can be forced to show by AForce parameter.
207204
208205`procedure MouseToCell(AMouse: TPoint; out ACol, ARow: Integer);`
209206
210207Returns indexes of a cell placed on the coordinates AMouse.
211208
212209`procedure SaveColumnsToXML(AXMLDoc: TXMLDocument; AColumnsNode: TDOMNode; AXMLFlags: TXMLColFlags = cXMLColFlagsAll); overload;`\
213210`procedure SaveColumnsToXML(AFileName: string; AColumnsNode: DOMString; AXMLFlags: TXMLColFlags = cXMLColFlagsAll); overload;`
214211
215212Saves parameters of columns.
216213
217214`procedure SaveToCSVFile(AFileName: string; ADelimiter: Char = ',';  AHeaders: Boolean = True; AVisibleColsOnly: Boolean = False);`
218215
219216Saves content of the grid to the text file.
220217
221218`procedure SelectCell(ACol, ARow: Integer; ASelectEditor: TEditorSelection; AForce: Boolean = False);`
222219
223220Changes selection to the cell specified by ACol and ARow. If column is invisible, it can be forced to show by AForce parameter. ASelectEditor specifies whether Editor will be opened (esNative depends on the egoAlwaysShowEditor).
224221
225222`procedure UpdateCell(ACol, ARow: Integer);`
226223
227224Updates single cell.
228225
229226`procedure UpdateColumn(ACol: Integer; AHeader: Boolean = False; AData: Boolean = True);`
230227
231228Updates cells in column.
232229
233230`procedure UpdateRow(ARow: Integer; AFixedCols: Boolean = False; AData: Boolean = True);`
234231
235232Updates visible cells in row.
236233
237234`procedure UpdateRowCount;`
238235
239236Call if you need to change number of data rows immediately. That's because RowCount is taken from OnGetDataRowCount event.
240237
241238## Workflow
242239
243240ECGrid itself does not contain any data (except column titles) and does not provide any built-in editor. All columns must be defined, setting ColCount (as it is with TStringGrid and TDrawGrid) is not enough, this property is read-only here.
244241
245242### Displaying data
246243
247244Lets assume that data are stored in any general structure, for example TFPGObjectList. For displaying all desired data is necessary:
248245
249246- Create corresponding number of columns
250247- Implement OnGetDataCellText event for each cell (for fixed columns too)
251248- Implement OnGetDataRowCount event. Implementation of this event will return TFPGObjectList.Count (ADataRowCount parameter)
252249
253250Note: It is also possible to implement only one OnGetDataCellText event and assign it to all columns. This event has parameter AColumn, so it is possible to use **`case`**` AColumn.Index `**`of`** construction.
254251
255252### Editing data
256253
257254- Create corresponding editors
258255- Implement OnSelectEditor event
259256- Implement OnChange or OnEditingDone events of the editors
260257
261258Editing:
262259
263260- Select cell and open editor (TEdit, TSpinEdit etc.). Example of implementation of the OnSelectEditor event:
264261
265262`procedure TForm1.SelectEditor(Sender: TObject; ACol, ARow: Integer; var AEditor: TWinControl; AKey: Word);`\
266263`begin`\
267-`  SpinEdit1.Value:=`<span style="color: blue">`{ fill appropriate value here }`</span>`;`\
264+`  SpinEdit1.Value:=``{ fill appropriate value here }``;`\
268265`  SpinEdit1.BoundsRect:=TECGrid(Sender).CellRectEditor(ACol, ARow);`\
269266`  AEditor:=SpinEdit1;`\
270267`end;`
271268
272269- Change value of the editor (text, number)
273270- Editor triggers OnChange or OnEditingDone event
274271- Event will change the data
275272- Call Grid.Invalidate to repaint, new data will appear
276273
277274Note: If number of items in list is changed (some items was added or deleted), it is necessary to call Grid.UpdateRows to display changes.
278275
279276### Order of columns
280277
281278**Important:** Option egoUseOrder must be enabled for proper use of Orders. Do not change egoUseOrder during lifetime of the component.
282279
283280There are properties to get the relation of Indexes and Orders.
284281
285282Property
286283
287284`Columns[i].Order;`
288285
289286stores initial order of each column (column with Index=i in this case), while property
290287
291288`ColOrd[i];`
292289
293290returns Index of column with Order=i.
294291
295292`CellOrd[i, j];`
296293
297294is the same as
298295
299296`Cell[ColOrd[i], j];.`
300297
301298Example
302299
303300Lets assume that grid has three columns and their are movable (option egoColMoving is enabled).
304301
305302| Index | 0 | 1 | 2 |
306303|-------|------|----------|-------|
307304| Title | Name | Category | Count |
308305| Order | 0 | 1 | 2 |
309306
310307If the first column (Name) is edited, grid will trigger event OnSelectEditor with parameter ACol=0.
311308
312309Now assume that user will move the first column to the third position.
313310
314311| Index | 0 | 1 | 2 |
315312|-------|----------|-------|------|
316313| Title | Category | Count | Name |
317314| Order | 1 | 2 | 0 |
318315
319316If the same column is edited again, grid will trigger event OnSelectEditor, but parameter ACol=2 now, which may be difficult to maintain.
320317
321318Even more, user wants to save defined order of columns when session is over and load it back in the next session (for example restart of an application).
322319
323320For this use case, there are properties ColOrd and CellOrd.
324321
325322If we use:
326323
327324`procedure TForm1.SelectEditor(Sender: TObject; ACol, ARow: Integer; var AEditor: TWinControl; AKey: Word);`\
328325`begin`\
329326`  case ACol of`\
330-`    0: `<span style="color: blue">`{ set editor for column Name }`</span>`;`\
331-`    1: `<span style="color: blue">`{ set editor for column Category }`</span>`;`\
332-`    2: `<span style="color: blue">`{ set editor for column Count }`</span>`;`\
327+`    0: ``{ set editor for column Name }``;`\
328+`    1: ``{ set editor for column Category }``;`\
329+`    2: ``{ set editor for column Count }``;`\
333330`  end;`\
334331`end;`
335332
336333This code works well as long as the columns are in their initial order.
337334
338335But we need to select the right editors for all columns, regardless of their new poitions. We must modify the code:
339336
340337`procedure TForm1.SelectEditor(Sender: TObject; ACol, ARow: Integer; var AEditor: TWinControl; AKey: Word);`\
341338`begin`\
342339`  case ColOrd[ACol] of`\
343-`    0: `<span style="color: blue">`{ set editor for column Name }`</span>`;`\
344-`    1: `<span style="color: blue">`{ set editor for column Category }`</span>`;`\
345-`    2: `<span style="color: blue">`{ set editor for column Count }`</span>`;`\
340+`    0: ``{ set editor for column Name }``;`\
341+`    1: ``{ set editor for column Category }``;`\
342+`    2: ``{ set editor for column Count }``;`\
346343`  end;`\
347344`end;         `
348345
349346There are methods LoadColumnsFromXML and SaveColumnsToXML which can store and refresh previous order of columns, but also their width and visibility (property Width and option ecoVisible).
350347
351348**Note:** When option egoUseOrder is disabled it reduces some calculations. Switch this property off if you extensively add and delete columns and/or if you disabled moving of columns (egoColMoving).
352349
353350---
354351*From the Free Pascal wiki, page "TECGrid", as it stood on 2017-03-29 after a change by Blaazen, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
355352Category: Components

Back to history