KB Article A1245
Visible to All Users

How to traverse grid rows in their visible order

Description:
My grid can be grouped. I need to write a routine to process all (group and data) rows in the order a user sees them in the grid. Can you help me with creating the relevant code?

Answer:
The following code iterates through all the visible grid rows.
If a grid is not grouped (the GroupedColumnCount is zero), rows with handles from 0 to DataRowCount - 1 are added to a list box.
If the grid is grouped, the loop uses group row handles, which are negative. The child data rows of a given group row are accessed via the GetChildRowHandle method.

C#
private static void TraverseRows(GridView gridView) { if (gridView.GroupCount == 0) for (int rowHandle = 0; rowHandle < gridView.RowCount; rowHandle++) { foreach (GridColumn gc in gridView.Columns) WriteString(String.Format("{0} ", gridView.GetRowCellDisplayText(rowHandle,gc))); WriteString("\r\n"); } else { // Get the list of grouped columns List<GridColumn> groupedColumnsList = new List<GridColumn>(); foreach (GridColumn groupedColumn in gridView.GroupedColumns) groupedColumnsList.Add(groupedColumn); for (int rowHandle = -1; gridView.IsValidRowHandle(rowHandle); rowHandle--) { WriteString(gridView.GetGroupRowDisplayText(rowHandle) + "\r\n"); if (gridView.GetChildRowHandle(rowHandle, 0) > -1) for (int childRowHandle = 0; childRowHandle < gridView.GetChildRowCount(rowHandle); childRowHandle++) { WriteString("-- "); foreach (GridColumn gc in gridView.Columns) { if (groupedColumnsList.Contains(gc)) continue; WriteString(String.Format("{0} ", gridView.GetRowCellDisplayText(gridView.GetChildRowHandle(rowHandle, childRowHandle), gc))); } WriteString("\r\n"); } } } }

See also:
Rows
Traversing Rows
Process Group Rows

Show previous comments (3)
DevExpress Support Team 11 years ago

    Hello,
    Unlike in C#, in VB the For loop does not have capability to operate a condition.  So, in VB, the While loop is an analog of the following code:

    C#
    for(int i = -1; view.IsValidRowHandle(i); i--)

    I hope this helps.

      Ok, thanks. So, it was my rust :).

      DevExpress Support Team 11 years ago

        You are welcome. Please contact us if you need further assistance.

        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.