Description:
I want to give the user to see a collumn showing the number of the row, like a ranking. How can I do this?
Answer:
This feature can be implemented using the OnGetDisplayText event of a column. Basically, you need to know the Index of a row painted and show it in a cell. Here is some sample code:
Delphiprocedure ColumnGetDisplayText(
Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
var AText: String);
var
Row: Integer;
begin
Row := Sender.GridView.DataController.GetRowIndexByRecordIndex(ARecord.RecordIndex, False);
AText := IntToStr(Row);
end;
However, this approach does not work when Grid is in grid mode, because in this mode, the data controller loads a fixed number of dataset records into memory. So, the ARecord.RecordIndex property does not return an actual record index. In this case, you can use the DataSet.RecNo property to calculate row index for the Grid records:
Delphiprocedure ColumnGetDisplayText(
Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
var AText: string);
var
AFocusedRecordIndex, ARecno: Integer;
begin
ARecno := TcxGridDBTableView(Sender.GridView).DataController.DataSource.DataSet.RecNo;
AFocusedRecordIndex := TcxGridDBTableView(Sender.GridView).Controller.FocusedRecordIndex;
AText := IntToStr( ARecno - AFocusedRecordIndex + ARecord.Index);
end;
See also:
A95: How to display an image from a data table in an Unbound View
A1095: How to set up an unbound item in a data-aware View
The solution does not work in a master-detail relationship. Adding records in detail grid,especially if it is empty, causes, after few inserts,records to dissapear from detail view.
Hello svismo,
I've created a separate ticket on your behalf (T395134: Showing a row number value in Master-Detail Grid). It has been placed in our processing queue and will be answered shortly.