What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleSearch for values in a FlexGrid control and sort it by columns
KeywordsFlexGrid, search, sort
CategoriesControls, Algorithms
To search for values, simply loop through the grid looking for the target value. Use the TopRow property to ensure that the row is visible after you find it.
Private Sub Command1_Click()
Dim target_name As String
Dim r As Integer

    ' Get the name.
    target_name = InputBox("Name", "Name", "")
    If Len(target_name) = 0 Then Exit Sub

    ' Search for the name, skipping the column heading row.
    target_name = LCase$(target_name)
    For r = 1 To MSFlexGrid1.Rows - 1
        If LCase$(MSFlexGrid1.TextMatrix(r, 0)) = _
            target_name Then
            ' We found the target. Select this row.
            MSFlexGrid1.Row = r
            MSFlexGrid1.RowSel = r
            MSFlexGrid1.Col = 0
            MSFlexGrid1.ColSel = MSFlexGrid1.Cols - 1

            ' Make the row visible.
            MSFlexGrid1.TopRow = r
            Exit Sub
        End If
    Next r

    ' We didn't find it.
End Sub
To sort by columns, use the MouseRow property to see if the user clicked the control's column header. Use MouseCol to see which column was clicked.

Store the selected column number in m_SpotColumn. If this is the same as the previously selected column, toggle the m_SortAscending variable that indicates whether the control is sorting in ascending or descending order. If this is a new column, restore the previous column's heading and select ascending order for the new column.

After you know which column should be sorted and how, select the column and use the Sort property to sort it.

This example uses a numeric sort for street address, zip code, and phone number because they start with numbers. This makes numbers with fewer digits come before numbers with more digits. For example, "8 Main St" < "10 1st". If you sort these alphabetically, "10 1st" < "8 Main St" because "1" < "8".

Private Sub MSFlexGrid1_Click()
Dim txt As String

    ' See if the user clicked row 0.
    If MSFlexGrid1.MouseRow > 0 Then Exit Sub

    ' See if this is the same column.
    If MSFlexGrid1.MouseCol = m_SortColumn Then
        ' This is the current sort column.
        ' Change the sort order and the column title.
        m_SortAscending = Not m_SortAscending
        If m_SortAscending Then
            MSFlexGrid1.TextMatrix(0, m_SortColumn) = _
                "> " & Mid$(MSFlexGrid1.TextMatrix(0, _
                    m_SortColumn), 3)
            MSFlexGrid1.TextMatrix(0, m_SortColumn) = _
                "< " & Mid$(MSFlexGrid1.TextMatrix(0, _
                    m_SortColumn), 3)
        End If
        ' This is a new sort column.
        ' Restore the previous sorting column's name.
        If m_SortColumn >= 0 Then
            MSFlexGrid1.TextMatrix(0, m_SortColumn) = _
                Mid$(MSFlexGrid1.TextMatrix(0, _
                    m_SortColumn), 3)
        End If

        ' Save the new sort column.
        m_SortColumn = MSFlexGrid1.MouseCol

        ' Sort using the new column.
        m_SortAscending = True
        MSFlexGrid1.TextMatrix(0, m_SortColumn) = _
            "> " & MSFlexGrid1.TextMatrix(0, m_SortColumn)
    End If

    MSFlexGrid1.Row = 1
    MSFlexGrid1.RowSel = MSFlexGrid1.Rows - 1
    MSFlexGrid1.Col = m_SortColumn

    If m_SortAscending Then
        Select Case m_SortColumn
            Case 2, 5, 6
                MSFlexGrid1.Sort = flexSortNumericAscending
            Case Else
                MSFlexGrid1.Sort = flexSortStringAscending
        End Select
        Select Case m_SortColumn
            Case 2, 5, 6
                MSFlexGrid1.Sort = flexSortNumericDescending
            Case Else
                MSFlexGrid1.Sort = flexSortStringDescending
        End Select
    End If
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.