Skip group rows when adding recordnumbers
-
05-07-2021 - |
Pregunta
I have the following code to add 'recordnumbers' in a cxGrid but it has the unwanted sideefeect that if it is a grid with grouping then it step one number every time there is a GroupRow and then there is a missing number. Any ways to avoid this
procedure TfrmProjectTasksActive.colCountGetDisplayText(Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord; var AText: string);
var
Row: integer;
begin
Row := Sender.GridView.DataController.GetRowIndexByRecordIndex(aRecord.RecordIndex, False);
aText := Format('%.*d', [3, (Row + 1)]);;
end;
Solución
Subtracting the number of groups before the row should work then, something like:
procedure TfrmProjectTasksActive.colCountGetDisplayText(Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord; var AText: string);
var
Row, GrIdx: integer;
begin
Row := Sender.GridView.DataController.GetRowIndexByRecordIndex(aRecord.RecordIndex, False);
GrIdx := Sender.GridView.DataController.Groups.DataGroupIndexByRowIndex[Row] + 1;
aText := Format('%.*d', [3, (Row + 1 - GrIdx)]);
end;
You need the +1
in the GrIdx
because when there is no groups then group index is -1
, first group has a index 0
and so on.
Otros consejos
This should work (not tested, written out of memory...):
procedure TfrmProjectTasksActive.colCountGetDisplayText(Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord; var AText: string);
var
Row: integer;
aRowInfo : TcxRowInfo;
begin
aRowInfo := ARecord.GridView.DataController.GetRowInfo(ARecord.Index);
if not (aRowInfo.Level < ARecord.GridView.DataController.Groups.GroupingItemCount) then //test, if Row is not a group-row
begin
Row := Sender.GridView.DataController.GetRowIndexByRecordIndex(aRecord.RecordIndex, False);
aText := Format('%.*d', [3, (Row + 1)]);
end;
end;
Hope I have it in my mind correctly, sorry if it does not work out of the box. But the "aRowInfo" should at least give you the right hint... Also: pay attention to the difference between "Index" and "RecordIndex" of the "ARecord".