Sign in Join
Wiki copytemporary

TStringGrid: what changed

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

OldNewLine
11**TStringGrid** ![](/wiki-images/6/67/Tstringgrid.png) is a component on the [[Additional tab]] of the [[Component Palette]]. A stringgrid provides a tabular display of textual information that may be edited as well.
22
33## Example StringGrid Program
44
55To create this example create a new project in Lazarus. Select TStringGrid to add to the form, by clicking the TStringGrid component from the menu area or component window, then click on the form. In this case 2 [[TButton|TButtons]] were also selected and dropped onto the form as shown. In this example it is also necessary to select a [[TOpenDialog]] component and drop it onto the form.
66
77![](/wiki-images/7/7c/TStringGrid02.png)
88
99## Adjusting Columns & Changing Their Properties
1010
1111Columns can be easily by added by right mouse clicking 'Columns: TGridColumns':
1212
1313![](/wiki-images/f/f0/TStringGrid09.png)
1414
1515By selecting AddItem, a new column is shown below. Under the Properties tab of the Object Inspector a new list of Properties and Events relating to that column are then shown. From here the Names of the columns are set along with the width. When finished, the TreeView looks like this:
1616
1717![](/wiki-images/3/39/TStringGrid10.png)
1818
1919In this example, Button1's name was changed to ButtonAddFiles and Button2's name was changed to ButtonExit. The StringGrid1 was stretched out and the buttons were aligned as shown. Note that there is a row and a column that are of a different color. That state illustrates the concept that this row and column could be for title labels for their respective column or row. Of course this default state can be changed simply by changing the 'FixedCols' or 'FixedRows' in the Object Inspector.
2020
2121![](/wiki-images/d/de/TStringGrid03.png)
2222
2323You can see in this case that the title lines have been changed and the StringGrid1 component has been anchored. This was achieved by taking two steps. The first one involves looking at the Object Inspector and selecting various properties as needed. One step that should be taken when starting out is to carefully note the default properties. When you have made numerous changes and need to go back, it is much easier if you know what their state was when you started or at various points along the way. The state of these properties in the last image illustrates only a single fixed row with column titles. This state illustrates:
2424
25-`  FixedCols[0], `\
26-`  FixedRows[1], `\
27-`  HeaderHotZones[gzFixedCols], `\
28-`  HeaderPushZones[gzFixedCols], `\
29-`  Options[goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRangeSelect,goSmoothScroll], `\
30-`  TitleFont[Color[clPurple]], `\
31-`  Style[fsBold], and `\
32-`  RowCount = 1. `
25+```text
26+ FixedCols[0],
27+ FixedRows[1],
28+ HeaderHotZones[gzFixedCols],
29+ HeaderPushZones[gzFixedCols],
30+ Options[goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRangeSelect,goSmoothScroll],
31+ TitleFont[Color[clPurple]],
32+ Style[fsBold], and
33+ RowCount = 1.
34+```
3335
3436After viewing your work by clicking the Run button on Lazarus you may find it desirable to change these properties. In this case, the additional properties of ColClickSorts and AlternateColor were selected.
3537
3638The second thing that can be done is to use the Anchor Editor (View--\>AnchorEditor) to link the sides of the StringGrid to the main form.
3739
3840## Using Available Predefined Properties
3941
4042At the bottom of the Object Inspector you can find useful information about the properties shown as seen in this example:
4143
4244![](/wiki-images/e/ea/TStringGrid04.png)
4345
4446To add information to the StringGrid1 component, it is necessary to either add data from a TStream, LoadCVSFile, link the grid to a database or other similar actions. If linking to a database there are other components that should be considered like the TDBGrid. Other components such as the OpenDialog may also assist using methods like the LoadCVSFile. In many cases, it is necessary to either directly link data to given cells or ranges. In our example, we will use the InsertRowWithValues method. It is now necessary to add to the ButtonAddFiles an Event by clicking the Events tab of the Object Inspector and selecting the 'OnClick' event.
4547
4648![](/wiki-images/f/f0/TStringGrid05.png)
4749
4850## Program Block To Add Data To The StringGrid
4951
5052By clicking on the OnClick the SourceEditor should have added a code block for the ButtonAddFilesClick procedure. To this you should add the following code:
5153
5254``` pascal
5355 uses
5456 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
5557 Grids, LazFileUtils, LazUtf8;
5658 . . . .
5759 types
5860 { TForm1 }
5961 TForm1 = class(TForm)
6062 ButtonAddFiles : TButton;
6163 . . . .
6264 procedure ButtonAddFilesClick(Sender : TObject);
6365 procedure ButtonExitClick(Sender : TObject);
6466 . . . .
6567 var
6668 Form1 : TForm1;
6769 implementation
6870 {$R *.lfm}
6971 { TForm1 }
7072 procedure TForm1.ButtonExitClick(Sender : TObject);
7173 begin
7274 Close;
7375 end;
7476 .
7577 procedure TForm1.ButtonAddFilesClick(Sender : TObject);
7678 var
7779 FilePathName : string;
7880 begin
7981 if OpenDialog1.Execute then
8082 FilePathName := OpenDialog1.Filename;
8183 AddFilesToList(FilePathName);
8284 end;
8385```
8486
8587The procedure for closing the application is shown for completeness. In the ButtonAddFilesClick procedure we now use the OpenDialog1 and select the Execute method. If is shown in a if - then statement where the boolean property of execute is tested. This method by default is true so the following line then when executed gives the OpenDialog1 property 'FileName' to our variable 'FilePathName'.
8688
8789In the last line of procedure a new procedure is shown for 'AddFilesToList'. We now need to create this procedure. In the type declaration under either 'Public' or 'Private' we need to add this new procedure. Under implementation, the code block for the procedure is created. In this example we are going to use the files of a DVD as can be seen in this illustration:
8890
8991![](/wiki-images/d/dc/TStringGrid06.png)
9092
9193We want these files to be listed upon StringGrid1.
9294
9395``` pascal
9496 Type
9597 . . . .
9698 private
9799 procedure AddFilesToList(FilePathName : String);
98100 . . . .
99101 procedure TForm1.AddFilesToList(FilePathName : String);
100102 var
101103 D, R, K : integer;
102104 FileName, FilePath : string;
103105 SearchRec1, SearchRec2 : TSearchRec;
104106 FileListVideo, FileListVst : TStringList;
105107 begin
106108 FileListVideo := TStringList.Create;
107109 FileListVst := TStringList.Create;
108110 FileName := ExtractFileName(FilePathName);
109111 FilePath := ExtractFilePath(FilePathName);
110112 FileListVideo := FindAllFiles(FilePath,'VIDEO_TS.*',true, faDirectory);
111113 R := 1;
112114 K := 0;
113115 for D := 0 to FileListVideo.Count -1 do
114116 begin
115117 if FindFirstUtf8(FilePath, faAnyFile and faDirectory, SearchRec1)=0 then
116118 begin
117119 repeat
118120 With SearchRec1 do
119121 begin
120122 FileName := ExtractFileName(FileListVideo.Strings[D]);
121123 K := FileSizeUtf8(FileListVideo.Strings[D]);
122124 StringGrid1.InsertRowWithValues(R,['0', FileName, IntToStr(K)]);
123125 R := R + 1;
124126 end;
125127 until FindNextUtf8(SearchRec1) <> 0;
126128 end;
127129 FindCloseUtf8(SearchRec1);
128130 end;
129131 .
130132 FileListVst := FindAllFiles(FilePath, 'VTS_*.*', true, faDirectory);
131133 K := 0;
132134 for D := 0 to FileListVst.Count -1 do
133135 begin
134136 if FindFirstUtf8(FilePath, faAnyFile and faDirectory,SearchRec2)=0 then
135137 begin
136138 repeat
137139 With SearchRec2 do
138140 begin
139141 FileName := ExtractFileName(FileListVst.Strings[D]);
140142 K := FileSizeUtf8(FileListVst.Strings[D]);
141143 StringGrid1.InsertRowWithValues(R,['1', FileName, IntToStr(K)]);
142144 R := R + 1;
143145 end;
144146 until FindNextUtf8(SearchRec2) <> 0;
145147 end;
146148 FindCloseUtf8(SearchRec2);
147149 end;
148150 StringGrid1.SortColRow(true, 1,1,StringGrid1.RowCount-1);
149151 FileListVst.Free;
150152 FileListVideo.Free;
151153 end;
152154```
153155
154156This example uses the methods 'FindAllFiles' from 'FileUtils' and 'FindFirstUtf8', 'FindNextUtf8' and 'FindCloseUtf8' from 'LazFileUtils'.
155157
156158## CheckBox Column
157159
158160One feature that can be very helpful in applications of the TStringGrid is having a checkbox column that either a user can click to signify their selection or used to show a certain state for some property. Adding this type of column is illustrated in this code. In the method 'InsertRowWithValues', the first column has '0' shown in the first part of the code when files containing 'VIDEO_TS*' are being selected. The '0' is illustrating the boolean state of a checkbox with 1 being checked and 0 unchecked. In selecting from the Object Inspector under StringGrid1--\>Columns: TGridColumns--\>0-Select in the TreeView, the ValueChecked and ValueUnchecked are shown. You can use other numbers, or have added code to change the state.
159161
160162## InsertRowWithValues Method
161163
162164In this example the method InsertRowWithValues is used to add data to our StringGrid1 component. Each column's data is entered followed by a comma. It may be necessary to use typecasting functions to get data into a string format. Variables can be referenced, or simply shown as text as our '0' and '1' are for checkbox column.
163165
164166![](/wiki-images/d/d3/TStringGrid08.png)
165167
166168Upon running our new application and clicking the button 'AddFiles' a dialog box opens allowing us to select file(s) to be added. If you click on a column header, StringGrid1 is sorted in the direction as shown by the green arrow. The result should be as shown in the following illustration:
167169
168170![](/wiki-images/e/e4/TStringGrid07.png)
169171
170172Depending on your needs other properties can be selected to allow editing, resizing columns, etc.
171173
172174## Content based row background coloring
173175
174176With PrepareCanvas event, you can alter the background color of every row based on the row's text content.
175177
176178![](/wiki-images/6/61/Stringgridcoloring.png)
177179
178180``` pascal
179181const
180182 csPASSED = 'Passed';
181183 csFAILED = 'Failed';
182184 csERROR = 'Error';
183185
184186procedure TForm1.StatisticsGridPrepareCanvas(Sender: TObject; aCol,
185187 aRow: Integer; aState: TGridDrawState);
186188begin
187189 if not(sender is TStringGrid) then Exit;
188190
189191 if (sender as TStringGrid).Rows[aRow].Text.Contains(csERROR) then begin
190192 (sender as TStringGrid).Canvas.Brush.Color := clRed;
191193 end else if (sender as TStringGrid).Rows[aRow].Text.Contains(csFAILED) then begin
192194 (sender as TStringGrid).Canvas.Brush.Color := clYellow;
193195 end else if (sender as TStringGrid).Rows[aRow].Text.Contains(csPASSED) then begin
194196 (sender as TStringGrid).Canvas.Brush.Color := clGreen;
195197 end;
196198end;
197199```
198200
199201## See also
200202
201203- [[TDrawGrid]]
202204- [[TDBGrid]]
203205- [[Grids Reference Page]] (for more details on grids in Lazarus)
204206
205207---
206208*From the Free Pascal wiki, page "TStringGrid", as it stood on 2023-05-17 after a change by Bart, 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).*

Back to history