Friday, July 20, 2012

Drag, Drop and Retain It Part II - Grid Columns


Because of the sample about drag, drop and retaining positions I posted yesterday, a new request has been made inside the forum and this time it is about a grid rearranging columns on the ply and retaining that settings.

I am thorn between naming this Grid Tricks # 6 because it involves grid tricks but in the end I decided to make this part 2 of drag and drop samples.

Anyway, here is how to do that.  And with retaining settings, you should always have something to save those first and retrieve later so a table is my best bet.  This is just simple as well to serve as your guide.  Full blown need may mean you have to save as well the form name and the grid name.

Here is the sample codes should you be interested:


Local oForm
oForm=Newobject("Form1")
oForm.Show
Read Events
Return

Define Class Form1 As Form
      Height = 390
      Width = 500
      AutoCenter = .T.
      Caption = 'Rearrange Columns and Retain It'
      ShowTips = .T.

      Add Object label1 As Label With ;
            Caption = 'Drag and Drop Columns to rearrange.  Then close and '+;
            'reopen form to see if the arrangement is saved',;
            Top = 10, Left = 10, Width = 480, Height = 40, WordWrap = .T.

      Add Object grid1 As Grid With ;
            GridLines = 0, ;
            Height = 328, ;
            Left = 10, ;
            Top = 50, ;
            Width = 480,;
            GridLines = 3,;
            DeleteMark = .F.,;
            GridLineColor = Rgb(192,192,192),;
            FontName = 'Tahoma',;
            FontSize = 8, Anchor = 15, AllowCellSelection = .F.,;
            ToolTipText = 'Drag Column to Rearrange'

      Procedure Load
            Set Talk Off
            Set Safety Off
            Close Databases All
            Select  company,contact,Title From (Home(2)+"data\customer");
              Where Recno() < 50 Into Cursor junk
            Use In Select('customer')

            * Create/use a table to store grid column settings
            If !File("gridcolumns.dbf")
                  Create Table gridcolumns (ColName c(10), ColOrder I)
            Else
                  Use gridcolumns In 0 Shared
            Endif
      Endproc

      Procedure Init
            If Reccount("gridcolumns") == 0
                  * First run? Save the current order of the columns
                  Local lnloop
                  With Thisform.grid1
                        For lnloop = 1 To .ColumnCount
                              lcColName =  .Columns(m.lnloop).Name
                              lnOrder = .Columns(m.lnloop).ColumnOrder
                              Insert Into gridcolumns Values (m.lcColName, m.lnOrder)
                        Next
                  Endwith
            Endif
            This._getorder()
      Endproc

      Procedure grid1.Init
            With This
                  .RecordSourceType = 6
                  .RecordSource = 'junk'
                  .Column1.Header1.Caption = 'Column1'
                  .Column2.Header1.Caption = 'Column2'
                  .Column3.Header1.Caption = 'Column3'
                  .SetAll('Width',150,'Column')

                  * Bind Move Events
                  For lnloop = 1 To .ColumnCount
                        Bindevent(.Columns(m.lnloop),'Moved',Thisform,'_Moved')
                  Next
            Endwith
      Endproc

      Procedure _Moved
            * Save new column orders
            Local lnloop
            With Thisform.grid1
                  For lnloop = 1 To .ColumnCount
                        lcColName =  Alltrim(.Columns(m.lnloop).Name)
                        lnOrder = .Columns(m.lnloop).ColumnOrder
                        Replace ColOrder With m.lnOrder For Alltrim(ColName);
                           = m.lcColName In gridcolumns
                  Next
            Endwith
      Endproc

      Procedure _getorder
            * Fetch Column Orderings
            Select gridcolumns
            Local lnloop
            With Thisform.grid1
                  For lnloop = 1 To .ColumnCount
                        lcColName =  .Columns(m.lnloop).Name
                        Locate For ColName = m.lcColName
                        .Columns(m.lnloop).ColumnOrder = ColOrder
                  Next
                  .Refresh
            Endwith
      Endproc

      Procedure Destroy
            Close Databases All
            Clear Events
      Endproc

Enddefine



2 comments:

  1. Very practical and easy your code. Very good job!

    ReplyDelete
  2. Thank you Luis,

    The simple the coding is, the better I like it. :-)

    ReplyDelete