Source code for Issue Number 92

Copyright 1997-2000 by C&D Programming Corp. All Rights Reserved. Source code may not be reproduced except for use in a compiled executable. All rights reserved. If you would like to reprint any or all of this code please email us at info@codeoftheweek.com

Code of the Week Home


Source Code

Just paste this code into any module and change the name of the module to basApexGrid.

'----------------------------------------------------------------------
'
'   Module Name:    basApexGrid
'   Written By:     C&D Programming Corp.
'   Create Date:    3/99
'   Copyright:      Copyright 1999 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'
'
'----------------------------------------------------------------------
Option Explicit

'
'   This routine will freeze lHowManyColumns on the left side of a grid.  It
'   is very useful if you have many columns in your grid and need to keep a few
'   important fields visible at all times.
'
Public Sub FreezeColumnsOnLeft(grd As TDBGrid, lHowManyColumns As Long)
    Dim Cols As TrueDBGrid50.Columns
    Dim C As TrueDBGrid50.Column
    Dim x As Long

    Dim s As TrueDBGrid50.Split

    ' split number 0 will be the split that contains the frozen columns
    ' split number 1 will be the split that contains the non-frozen (scrollable) columns
    Set s = grd.Splits.Add(0) ' Create split

    ' make all columns in split visible first
    Set Cols = s.Columns
    For Each C In Cols
        C.Visible = False
    Next C

    ' make frozen columns in split one invisible
    For x = 0 To lHowManyColumns - 1
        Cols(x).Visible = True
    Next

    ' Configure new Split to display exactly lHowManyColumns columns, and
    ' disable resizing
    With s
        .SizeMode = dbgNumberOfColumns
        .Size = lHowManyColumns
        .AllowSizing = False
    End With

    ' make all columns frozen in split 0 and then make the same columns
    ' invisible in split 1
    Set Cols = grd.Splits(1).Columns
    For x = 0 To lHowManyColumns - 1
        Cols(x).Visible = False
    Next

    ' turn off record selectors in the scrollable split (this make everything look
    ' much better
    With grd.Splits(1)
        .RecordSelectors = False
    End With

    grd.TabAcrossSplits = True  ' allow arrow movement through splits
    DoEvents                    ' give grid time to update display.
    grd.Split = 0       ' force active split to be the one
                                ' with the frozen columns.
    grd.Col = 0         ' make the first column have focus
    grd.CurrentCellVisible = True
End Sub

'
'   This routine performs the opposite function of FreezeColumnsOnLeft
'   It assumes you have previously called FreezeColumnsOnLeft.  If you haven't
'   and you have not defined additional splits nothing will happen.  If you have
'   defined additional splits they will be removed.
'
Public Sub UnfreezeColumnsOnLeft(grd As TDBGrid)
    Dim Cols As TrueDBGrid50.Columns
    Dim C As TrueDBGrid50.Column

    If grd.Splits.Count = 1 Then
        Exit Sub
    Else
        ' remove the left most split
        grd.Splits.Remove 0
        DoEvents
        Set Cols = grd.Splits(0).Columns
        For Each C In Cols
            C.Visible = True
        Next C
        grd.Split = 0
        grd.Col = 0
        grd.CurrentCellVisible = True
    End If
End Sub