ComponentOne True DBGrid Pro 8
Tutorial 20 - Sorting and Searching

In this tutorial, you will learn how to use the sorting and searching features of the XArrayDB object. The QuickSort method sorts a range of rows in an XArrayDB object according to one or more columns (up to ten). The Find method searches for a specified value within a column of an XArrayDB object, starting at a particular row

  1. Start with a project created in Tutorial 19.

  2. Place the following controls on the form (Form1) as shown in the following figure: two text box controls (Text1 and Text2), a command button (Command1), and two labels (Label1 and Label2).

    Initializing the array data

    Next, initialize the XArrayDB object so that it contains 100 rows and 4 columns of random integers.

  3. Place the following code in the Form_Load event:

    Example Title
    Copy Code
    Private Sub Form_Load()
    
        ' Allocate space for 100 rows, 4 columns.
    
        x.ReDim 0, 99, 0, 3
    
     
    
        Dim row As Long, col As Integer
    
     
    
        ' The LowerBound and UpperBound properties correspond to the LBound and
    
        ' UBound functions in Visual Basic. Hard-coded dimensions can be used
    
        ' instead, if known.
    
        For row = x.LowerBound(1) To x.UpperBound(1)
    
            For col = x.LowerBound(2) To x.UpperBound(2)
    
                x(row, col) = CInt(99 * Rnd + 1)
    
            Next col
    
        Next row
    
     
    
        ' Bind True DBGrid Control to this XArrayDB instance.
    
        Set TDBGrid1.Array = x
    
       
    
        ' Enable footers.
    
        TDBGrid1.ColumnFooters = True
    
       
    
        ' Display headers and footers as buttons.
    
        Dim obcol As TrueDBGrid80.Column
    
        For Each obcol In TDBGrid1.Columns
    
            obcol.ButtonFooter = True
    
            obcol.ButtonHeader = True
    
        Next obcol
    
    End Sub
    
  4. Add the following code to Command1:

    Example Title
    Copy Code
    Private Sub Command1_Click()
    
        Dim RowFound As Long
    
     
    
        ' Execute Find.
    
        RowFound = x.Find(x.LowerBound(1), CInt(Text2.Text), _
    
                     CInt(Text1.Text), _
    
                     XORDER_ASCEND, XCOMP_EQ, XTYPE_NUMBER)
    
     
    
        ' Successful Find will return a row number. Set focus to the row and
    
        ' column.
    
        If RowFound >= 0 Then TDBGrid1.Bookmark = RowFound
    
        TDBGrid1.col = CInt(Text2.Text)
    
        TDBGrid1.SetFocus
    
    End Sub
    
  5. Add the following code to the HeadClick and FootClick events:

    Example Title
    Copy Code
    Private Sub TDBGrid1_HeadClick(ByVal ColIndex As Integer)
    
        ' Ascending sort.
    
        x.QuickSort x.LowerBound(1), x.UpperBound(1), ColIndex, _
    
            XORDER_ASCEND, XTYPE_INTEGER
    
        TDBGrid1.Refresh
    
    End Sub
    
    Private Sub TDBGrid1_FootClick(ByVal ColIndex As Integer)
    
        ' Descending sort.
    
        x.QuickSort x.LowerBound(1), x.UpperBound(1), ColIndex, _
    
            XORDER_DESCEND, XTYPE_INTEGER
    
        TDBGrid1.Refresh
    
    End Sub
    

Run the program and observe the following:

This concludes Tutorial 20.

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback