Ticket A767
Visible to All Users

How to show a row number value in an unbound Grid column

created 21 years ago (modified 2 years ago)

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:

Delphi
procedure 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:

Delphi
procedure 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

Comments (2)

    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.

    DevExpress Support Team 9 years ago

      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.

      Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

      Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.