Ticket T715387
Visible to All Users

How to add to cxGrid column with row number?

created 6 years ago

I found on your page that example how to fill unbound cxGrid Column to auto number that column. But when I sort that grid on some other column - column with auto number gets messed up values. Those values should always remain filled sorted like auto inc column in a dataset. Is there a way to modify that code to achieve that behaviour?

Delphi
procedure TForm.cxGrid1DBTableView1Column1GetDataText( Sender: TcxCustomGridTableItem; ARecordIndex: Integer; 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 + ARecordIndex); end;

Answers approved by DevExpress Support

created 6 years ago (modified 6 years ago)

Hello,

As I see, the code you provided is for a data-aware GridView (not unbound). Nevertheless, if you wish to populate an unbound column by the sequence of values ("1", "2","3", etc), you use the Column.OnGetDataText event.
Use its ARecordIndex parameter in the DataController.GetRowIndexByRecordIndex method to find the current Row and set this index to the AText parameter:

Delphi
procedure <AForm>.<AcxGridTableViewColumn>GetDataText(Sender: TcxCustomGridTableItem; ARecordIndex: Integer; var AText: string); var AIndex: Integer; begin AIndex := TcxGridTableView(Sender.GridView).DataController.GetRowIndexByRecordIndex(ARecordIndex, False); AText := IntToStr(AIndex + 1); end;
    Comments (1)

      Thank you for fast and accurate answer. It works as expected.

      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.