分类: 敏捷开发
2014-06-24 17:09:28
AdvStringGrid的分组功能
procedure Group(ColIndex:integer);
procedure UnGroup;
property GroupColumn:integer;
procedure SubGroup(ColIndex:integer);
The Group method is equivalent to
assignment of the GroupColumn property, ie :
AdvStringGrid.Group(5) has the same effect as AdvStringGrid.GroupColumn := 5;
Note that the column for grouping can only start from column 1, since column 0 is the placeholder for the expand / contract nodes. The GroupColumn property has the additional benefit that it returns -1 when grouping is not active. Otherwise it returns the current grouped column.
Example: loading a CSV file, applying
grouping and performing a grouped sort
// loading CSV file in normal cells AdvStringGrid1.SaveFixedCells := False;
AdvStringGrid1.LoadFromCSV('cars.csv'); // automatically adapt column width to
max. text width in columns
AdvStringGrid1.AutoSizeColumns(False,10); // insert column as placeholder for
nodes
AdvStringGrid1.InsertCols(0,1); // setting width of node column to fixed width
of 20
AdvStringGrid1.ColWidths[0] := 20;
// do grouping on column 1
AdvStringGrid1.GroupColumn := 1;
// apply grouped sorting on (new) column 1
AdvStringGrid1.SortSettings.Column := 1;
AdvStringGrid1.QSortGroup;
When a grouped view is no longer
necessary, it can be removed by:
AdvStringGrid.UnGroup;
Some extra capabilities for more
visually appealing grouping can be set through the property grid.Grouping.
Through this property it can be enabled that group headers are automatically
set in a different color and that cells from a group header are automatically
merged. In addition, a group can also have a summary line. A summary line is an
extra row below items that belong to the same group. This summary line can be
used to put calculated group values in. The color for this summary line can
also be automatically set as well as cell merging performed on this.
Grouping property:
grid.GroupSum(AColumn: Integer);
//Calculates column sums per group
grid.GroupAvg(AColumn: Integer);
//Calculates column averages per group
Grid.GroupMin(AColumn: Integer);
//Calculates column minimum per group
Grid.GroupMax(AColumn: Integer); Calculates column minimum per group
//Grid.GroupCount(AColumn: Integer);
Calculates number of rows in a group for each group
Grid.GroupCustomCalc(AColumn: Integer);
//Allows to perform a custom calculation of group data with the event
OnGroupCalc
If there is a need for a special group calculation that is not available in the standard group calculation functions, the method grid.GroupCustomCalc can be used. For each group in the grid, this will trigger the event grid.OnGroupCalc(Sender: TObject; ACol, FromRow, ToRow: Integer; var Res: Double);
In this sample, the grid is initialized
with random number, is grouped on column 1 and for the first column in the
grouped grid the standard deviation is calculated:
procedure TForm1.AdvStringGrid1GroupCalc(Sender: TObject; ACol, FromRow,
ToRow: Integer; var Res: Double);
var i: integer; d, m, sd: double;
begin // calculate mean m := 0; for i := FromRow to ToRow do begin m := m +
advstringgrid1.Floats[ACol,i]; end;
m := m / (ToRow - FromRow + 1);
// calculate standard deviation
sd := 0; for i := FromRow to ToRow do
begin sd := sd + sqr(advstringgrid1.Floats[ACol,i] - m); end;
sd := sd / (ToRow - FromRow); Res := sqrt(sd); end;
procedure TForm1.FormCreate(Sender: TObject); var i: integer;
begin
AdvStringGrid1.RowCount := 100;
AdvStringGrid1.RandomFill(false,100);
for i := 1 to AdvStringGrid1.RowCount - 1 do
AdvStringGrid1.Ints[1,i] := random(5);
AdvStringGrid1.Grouping.Summary := true;
AdvStringGrid1.Grouping.MergeHeader := true;
AdvStringGrid1.Grouping.ShowGroupCount := true;
Advstringgrid1.Group(1);
Advstringgrid1.GroupCustomCalc(1);
end;
Subgroups
In this example, a first group is created by calling grid.Group(1). Two
additional subgroups are
added by calling grid.SubGroup(1) and grid.SubGroup(2);
The full code used to create this starting from a default grid is:
procedure TForm1.Button1Click(Sender: TObject);
begin
AdvStringGrid1.RowCount := 100;
AdvStringGrid1.Randomfill(false,3);
AdvStringGrid1.Group(1);
AdvStringGrid1.SubGroup(1);
AdvStringGrid1.SubGroup(2);
AdvStringGrid1.ColWidths[1] := 10;
AdvStringGrid1.ColWidths[2] := 10;
end;