Image of Navigational Map linked to Home / Contents / Search Sorting TrueGrid Using the Column Order

Stephen Archer - GUI Computing
Image of Line Break

The Configure property in TrueGrid (by Apex Software Corp.) allows the user to modify the column order (as it appears on the screen, by dragging the columns), as well as the column widths. This is a powerful feature - but what if your grid was bound to a data control, and you wanted to sort the grid depending on the column order that the user has selected?

My initial thought was that TrueGrid would trigger an event when the column order changed, however this was not the case.

My final solution involved recording the column order in an array (in the MouseDown Event), then checking this order in the MouseUp Event. When the column order has changed it is a simple case of re-sorting the recordset in the data control.

  Private Sub tgCustomers_MouseDown(Button As Integer, Shift As Integer, 
                                                        X As Single, Y As Single)
    Dim iColumnNo As Integer
    For iColumnNo = 1 To tgCustomers.Columns
      iOldColumnOrder(iColumnNo) = tgCustomers.ColumnOrder(1)
    Next iColumnNo
  End Sub

  Private Sub tgCustomers_MouseUp(Button As Integer, Shift As Integer, 
                                                     X As Single, Y As Single)
    Dim sSQL As String
    Dim iSortNo As Integer
    Dim iColumnNo As Integer
    If iOldColumnOrder(1) <> tgCustomers.ColumnOrder(1) Or iOldColumnOrder(2) 
                          <> tgCustomers.ColumnOrder(2) Or iOldColumnOrder(3) 
                          <> tgCustomers.ColumnOrder(3) Then
      Screen.MousePointer = 11
      sSQL = "SELECT Customer.CustomerID, Customer.CompanyName, 
                                          Customer.ContactName FROM Student ORDER BY "
      ' Now looping over the order of the columns 
      ' using the column field name to build the SQL.
      For iSortNo = 1 To tgCustomers.Columns
        For iColumnNo = 1 To tgCustomers.Columns
          If tgCustomers.ColumnOrder(iColumnNo) = iSortNo Then
            sSQL = sSQL & tgCustomers.ColumnField(iColumnNo)
            If iSortNo <> tgCustomers.Columns Then sSQL = sSQL & ", "
          End If
        Next iColumnNo
      Next iSortNo
      datCustomers.RecordSource = sSQL
      datCustomers.Refresh
      Screen.MousePointer = 0
    End If
  End Sub

One problem with this method is that the data control takes time to refresh - and if you have multiple columns, and the user wants to rearrange more than one of them, the data control will be refreshed more than once. A simple way around this is to create a button which toggles between 'Configure Grid' and 'Update Grid'. Therefore, instead of putting the code in the MouseDown and MouseUp events, we would place it in the button-click event and execute the code depending on the button's caption.

Remember to set the Configure property of the grid to FALSE when the user has finished configuring the grid.

TrueGrid Pro - and other software by APEX Software Corporation - is available in Australia from GUI Computing.



Written by: Stephen Archer
August '96

Image of Arrow linked to Previous Article Image of Arrow linked to Next Article
Image of Line Break
[HOME] [TABLE OF CONTENTS] [SEARCH]