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 BasicPrivate 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?
In order to be correct, please add the missing parameter i in the second vb function:
If row Is view.GetDataRow(i) Then
Hello Matt,
Thank you for pointing that out. I corrected the code.