Main Menu
Knowledge Base
Product Registration
Log an Incident
Request a Feature
Search Incidents/Bug Reports


Search KB

Please note: In an effort to better serve you, we are in the process of restructuring DevCenter. In the process, we have moved many items that you may be used to finding in DevCenter over to the Main Site. If you are having trouble locating something, please try looking at the following places:

Knowledge Base Article: KB07694

HOWTO:How can I loop through all the rows in the grid, including child rows?


The information in this article applies to:
UltraWinGrid (v5.2.20052)
  Article Created: 
6/21/2005

Last Updated:
2/23/2006

Article Type
How To
  
Page Options
Average Rating:
6 out of 10

Rate this page
Print this page
E-mail this page
Add to Favorites

Summary

Looping through the rows in the grid can be done recursively by first traversing the grid's Rows collection and then using the ChildBands property of each row to get it's children.

Additional Information

Note: This article applies mainly to versions of the UltraGrid prior to v6.1. In v6.1, a new method was added to the UltraGridBand and the UltraGridRowCollection. The new method is called GetRowEnumerator and it allows you to get an enumerator that spans multiple bands of the grid. See the "GetRowEnumerator" method in the help for more information and sample code.



Following code shows you how to traverse through all the rows in the UltraGrid. It finds out the number of regular rows (non-group-by rows) and number of group-by rows and shows the counts.


[In Visual Basic]

     Imports Infragistics.Shared
     Imports Infragistics.Win
     Imports Infragistics.Win.UltraWinGrid

    Private Sub Button40_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button40.Click

        Dim rowsCount As Integer = 0
        Dim groupByRowsCount As Integer = 0

        ' Call the helper method which is a recursive implmentation for traversing rows.
        MessageBox.Show("Please wait. This operation may take a while depending on number of rows.")
        Me.TraverseAllRowsHelper(Me.ultraGrid1.Rows, rowsCount, groupByRowsCount)

        ' Show a dialog showing the number of regular rows and number of group-by rows.
        MessageBox.Show("The UltraGrid has " & rowsCount & " number of regular rows, and " & groupByRowsCount & " number of group-by rows.")

    End Sub

    Private Sub TraverseAllRowsHelper(ByVal rows As RowsCollection, ByRef rowsCount As Integer, ByRef groupByRowsCount As Integer)

        ' Loop through every row in the passed in rows collection.
        Dim row As UltraGridRow = Nothing
        For Each row In rows

            ' If you are using Outlook GroupBy feature and have grouped rows by columns in the
            ' UltraGrid, then rows collection can contain group-by rows or regular rows. So you
            ' may need to have code to handle group-by rows as well.
            If TypeOf row Is UltraGridGroupByRow Then
                Dim groupByRow As UltraGridGroupByRow = DirectCast(row, UltraGridGroupByRow)
                ' Incremement the group-by row count.
                groupByRowsCount += 1
            Else
                ' Incremenent the regular row count.
                rowsCount += 1
            End If

            ' If the row has any child rows. Typically, there is only a single child band. However,
            ' there will be multiple child bands if the band associated with row1 has mupliple child
            ' bands. This would be the case for exmple when you have a database hierarchy in which a
            ' table has multiple child tables.
            If Not Nothing Is row.ChildBands Then

                ' Loop throgh each of the child bands.
                Dim childBand As UltraGridChildBand = Nothing
                For Each childBand In row.ChildBands
                    ' Call this method recursivedly for each child rows collection.
                    Me.TraverseAllRowsHelper(childBand.Rows, rowsCount, groupByRowsCount)
                Next
            End If

        Next

    End Sub

[In C#]

        using Infragistics.Shared;
        using Infragistics.Win;
        using Infragistics.Win.UltraWinGrid;
        using System.Diagnostics;

        private void button40_Click(object sender, System.EventArgs e)
        {

            int rowsCount = 0;
            int groupByRowsCount = 0;

            // Call the helper method which is a recursive implmentation for traversing rows.
            MessageBox.Show( "Please wait. This operation may take a while depending on number of rows." );
            this.TraverseAllRowsHelper( this.ultraGrid1.Rows, ref rowsCount, ref groupByRowsCount );

            // Show a dialog showing the number of regular rows and number of group-by rows.
            MessageBox.Show( "The UltraGrid has " + rowsCount + " number of regular rows, and " + groupByRowsCount + " number of group-by rows." );

        }
        
        private void TraverseAllRowsHelper( RowsCollection rows, ref int rowsCount, ref int groupByRowsCount )
        {

            // Loop through every row in the passed in rows collection.
            foreach ( UltraGridRow row in rows )
            {
                // If you are using Outlook GroupBy feature and have grouped rows by columns in the
                // UltraGrid, then rows collection can contain group-by rows or regular rows. So you
                // may need to have code to handle group-by rows as well.
                if ( row is UltraGridGroupByRow )
                {
                    UltraGridGroupByRow groupByRow = (UltraGridGroupByRow)row;

                    // Incremement the group-by row count.
                    groupByRowsCount++;
                }
                else                    
                {
                    // Incremenent the regular row count.
                    rowsCount++;
                }
                
                // If the row has any child rows. Typically, there is only a single child band. However,
                // there will be multiple child bands if the band associated with row1 has mupliple child
                // bands. This would be the case for exmple when you have a database hierarchy in which a
                // table has multiple child tables.
                if ( null != row.ChildBands )
                {
                    // Loop throgh each of the child bands.
                    foreach ( UltraGridChildBand childBand in row.ChildBands )
                    {
                        // Call this method recursivedly for each child rows collection.
                        this.TraverseAllRowsHelper( childBand.Rows, ref rowsCount, ref groupByRowsCount );
                    }
                }
            }

        }


How would you rate the quality of this content?
Poor -----------------------------------------> Outstanding

Tell us why you rated the content this way. (optional)