Description:
I would like to be able to determine which DataRow objects correspond to the records in a particular group. How can I do this?
Answer:
First, we should mention that it is impossible to just call the GetRow or GetDataRow method to obtain the DataRow object corresponding to a particular Group row. This is because a group row does not correspond to any DataRow object - group rows are specially created by the grid to visualize grouping only. You can easily distinguish between a group row and a data row by the value returned by the view's FocusedRowHandle property. For group rows it's negative.
The XtraGrid provides the GridView.GetChildRowCount and GridView.GetChildRowHandle methods which allow you to iterate through the child records in a group row and determine their rowhandles. Using these values you will be able to convert them into DataRow objects using either the GridView.GetDataRow or GridView.GetRow methods. Here is some sample code showing how this can be done:
C#private DataRow[] GetChildDataRows(GridView view, int rowHandle) {
if(rowHandle >= 0)
return null;
ArrayList r = new ArrayList();
GetChildDataRows(view, rowHandle, r);
DataRow[] result = new DataRow[r.Count];
r.CopyTo(result);
return result;
}
void GetChildDataRows(GridView view, int rowHandle, ArrayList r) {
int childCount = view.GetChildRowCount(rowHandle);
for(int i = 0; i < childCount; i++){
int childRowHandle = view.GetChildRowHandle(rowHandle, i);
if(childRowHandle < 0)
GetChildDataRows(view, childRowHandle, r);
else
r.Add(view.GetDataRow(childRowHandle));
}
}
See also:
How to traverse grid rows in their visible order
ms-help://MS.VSCC/DevExpress/DevExpress.XtraGrid/CustomDocument742.htm