Search code examples
vb.nettreeviewnodesc1flexgrid

FlexGrid Tree not creating nodes for entire grid


I have a flexgrid being populated by a datatable. I am then grouping by a specified column and adding nodes to use the treeview property. The issue is, for example, if I have 1000 records and it splits it up into 20 groups, I will have 20 records at the end that are just added into the last node because it stops looking after 1000 rows, INCLUDING the nodes that it created, meaning that it would think there are 1020 rows in the grid. Here is the function I am using to create the nodes:

Private Sub GroupBy(ByVal colName As String, ByVal level As Integer)
        Dim current As Object = ""
        For i As Integer = fgResults.Rows.Fixed To _data.Tables("Results").Rows.Count - 1
            If Not fgResults.Rows(i).IsNode Then
                Dim value As Object = fgResults.Item(i, colName).ToString
                If Not Object.Equals(value, current) Then
                    fgResults.Rows.InsertNode(i, level)
                    fgResults.Item(i, colName) = value
                    current = value
                End If
            End If
        Next
    End Sub

colName is the name of the column i am grouping by, level being the level of the node(I am always using 0 here as i only want one layer). What I am thinking is if there is a way to make the for loop run until the end of the datatable, that would be the best solution. Thank you for any help.


Solution

  • After more research and some collaboration with some developers, I found the oh so simple solution, so I will post it here since I was not able to find much research online about this topic. The issue was that the loop was not making it to the end of the grid, because as nodes were created, the number of rows increased. The solution to this was to add the same for loop before, but with a counter incrementing wherever a node will be placed. That way, when I run the for loop to place the nodes, I add the counter to the number of rows I am looping through so that it loops through the whole thing. That counter looks like this and was placed right before the for loop i have posted above.

            For i As Integer = fgResults.Rows.Fixed To _data.Tables("Results").Rows.Count - 1
                If Not fgResults.Rows(i).IsNode Then
                    Dim value As Object = fgResults.Item(i, colName).ToString
                    If Not Object.Equals(value, current) Then
                        count += 1
                        current = value
                    End If
                End If
            Next
    

    I then modified the for loop i have above to include this count: For i As Integer = 1 To _data.Tables("Results").Rows.Count + count