KB Article A1488
Visible to All Users

How to obtain a grid row handle by a data row object

Description:
The GridView class has the GetRow method, which returns a row object by a given GridView row handle. I need the opposite function, which returns a row handle by a given data row. Is there one?

Answer:
[Current versions]

Our current versions have an undocumented GridView.DataController.FindRowByRowValue method that allows finding a row handle by its value without iterating through the grid rows. The 14.2 version also has a documented GridView.FindRow method.
[Old versions]
There is no such method in the current version. It may be implemented in future grid versions - such a request is already in our suggestions list. Currently, you can use one of the following functions.

C#
private int FindRowHandleByRowObject(DevExpress.XtraGrid.Views.Grid.GridView view, object row) { if(row != null) for(int i = 0; i < view.DataRowCount; i++) if(row.Equals(view.GetRow(i))) return i; return DevExpress.XtraGrid.GridControl.InvalidRowHandle; } private int FindRowHandleByDataRow(DevExpress.XtraGrid.Views.Grid.GridView view, DataRow row) { if(row != null) for(int i = 0; i < view.DataRowCount; i++) if(view.GetDataRow(i) == row) return i; return DevExpress.XtraGrid.GridControl.InvalidRowHandle; }
Visual Basic
Private Function FindRowHandleByRowObject(ByVal view As DevExpress.XtraGrid.Views.Grid.GridView, ByVal row As Object) As Integer Dim i As Integer If Not row Is Nothing Then For i = 0 To view.DataRowCount - 1 If row.Equals(view.GetRow(i)) Then Return i End If Next End If Return DevExpress.XtraGrid.GridControl.InvalidRowHandle End Function Private Function FindRowHandleByDataRow(ByVal view As DevExpress.XtraGrid.Views.Grid.GridView, ByVal row As DataRow) As Integer Dim i As Integer If Not row Is Nothing Then For i = 0 To view.DataRowCount - 1 If row Is view.GetDataRow(i) Then Return i End If Next End If Return DevExpress.XtraGrid.GridControl.InvalidRowHandle End Function

See Also:
Identifying Rows and Cards
Can the GridView.ViewRowHandleToDataSourceIndex method be used when the grid's DataSource is a DataTable?

Comments (2)

    In order to be correct, please add the missing parameter i in the second vb function:

    Visual Basic
    If row Is view.GetDataRow(i) Then
    Sasha (DevExpress Support) 4 years ago

      Hello Matt,

      Thank you for pointing that out. I corrected the code.

      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.