The following example uses the getColumnOrder( ) method with a form's onOpen and onClose event handlers to store, and subsequently recall, a Grid's column order.

parameter bModal 

local f 

f = new testGridForm() 

if (bModal) 

   f.mdi = false // ensure not MDI 

   f.readModal() 

else 

   f.open() 

endif 

 

class testGridForm of FORM 

with (this) 

   onOpen = class::FORM_ONOPEN 

   onClose = class::FORM_ONCLOSE 

   height = 16 

   left = 29.4286 

   top = 3.6818 

   width = 78.4286 

   text = "" 

endwith 

this.PLUSSAMPLE1 = new DATABASE() 

this.PLUSSAMPLE1.parent = this 

with (this.PLUSSAMPLE1) 

   left = -1.1429 

   top = 0.5 

   databaseName = "PLUSSAMPLE" 

   active = true 

endwith 

this.FISH1 = new QUERY() 

this.FISH1.parent = this 

with (this.FISH1) 

   left = -1.1429 

   top = 0.5 

   database = form.plussample1 

   sql = "select * from fish.dbf" 

   active = true 

endwith 

this.GRID1 = new GRID(this) 

with (this.GRID1) 

   dataLink = form.fish1.rowset 

   columns["COLUMN1"] = new GRIDCOLUMN(form.GRID1) 

   columns["COLUMN1"].dataLink = form.fish1.rowset.fields["id"] 

   columns["COLUMN1"].editorType = 1 // EntryField 

   columns["COLUMN1"].width = 10.7143 

   columns["COLUMN2"] = new GRIDCOLUMN(form.GRID1) 

   columns["COLUMN2"].dataLink = form.fish1.rowset.fields["name"] 

   columns["COLUMN2"].editorType = 1 // EntryField 

   columns["COLUMN2"].width = 17.8571 

   columns["COLUMN3"] = new GRIDCOLUMN(form.GRID1) 

   columns["COLUMN3"].dataLink = form.fish1.rowset.fields["species"] 

   columns["COLUMN3"].editorType = 1 // EntryField 

   columns["COLUMN3"].width = 19.1429 

   columns["COLUMN4"] = new GRIDCOLUMN(form.GRID1) 

   columns["COLUMN4"].dataLink = form.fish1.rowset.fields["length cm"] 

   columns["COLUMN4"].editorType = 3 // SpinBox 

   columns["COLUMN4"].width = 10.2857 

   columns["COLUMN5"] = new GRIDCOLUMN(form.GRID1) 

   columns["COLUMN5"].dataLink = form.fish1.rowset.fields["description"] 

   columns["COLUMN5"].editorType = 1 // EntryField 

   columns["COLUMN5"].width = 11.7143 

   with (columns["COLUMN1"].headingControl) 

      value = "ID" 

   endwith 

   with (columns["COLUMN2"].headingControl) 

      value = "Name" 

   endwith 

   with (columns["COLUMN3"].headingControl) 

      value = "Species" 

   endwith 

   with (columns["COLUMN4"].headingControl) 

      value = "Length CM" 

   endwith 

   with (columns["COLUMN5"].headingControl) 

      value = "Description" 

   endwith 

   bgColor = "white" 

   rowSelect = true 

   hScrollBar = 1 // On 

   vScrollBar = 1 // On 

   height = 11.5 

   left = 1.2857 

   top = 2.7727 

   width = 76.2857 

endwith 

this.TEXTLABEL1 = new TEXTLABEL(this) 

with (this.TEXTLABEL1) 

   height = 1 

   left = 22.2857 

   top = 0.3182 

   width = 41 

   transparent = true 

   text = "Grid.getColumnRows() example" 

   colorNormal = "Blue" 

   fontSize = 14 

endwith 

this.TEXTLABEL2 = new TEXTLABEL(this) 

with (this.TEXTLABEL2) 

   height = 1 

   left = 22 

   top = 1.5909 

   width = 40.5714 

   text = "Move columns around, close form, re-run it ..." 

endwith 

this.rowset = this.fish1.rowset 

 

function form_onClose 

   // Store column order to an array: 

   aGridOrder = form.grid1.getColumnOrder() 

   // save to a table 

   if not file( "SaveGrid.dbf" ) 

      // it doesn't exist -- save it 

      create table SaveGrid ; 

      ( QueryName char( 20 ),; 

      FieldName char( 20 ) ) 

   else 

      // it does exist, empty it! 

      _app.databases[1].emptyTable( "SaveGrid" ) 

   endif 

   // create query: 

   qSaveGrid = new query() 

   qSaveGrid.sql = "select * from SaveGrid" 

   qSaveGrid.active := true 

   // loop through the grid's order and save it 

   for i = 1 to aGridOrder.size/2 // two columns 

      // column one is query name 

      // column two is field name 

      ? aGridOrder[i,1], aGridOrder[i,2] 

      qSaveGrid.rowset.beginAppend() 

      qSaveGrid.rowset.fields["QueryName"].value := aGridOrder[i,1] 

      qSaveGrid.rowset.fields["FieldName"].value := aGridOrder[i,2] 

   next 

   // cleanup 

   qSaveGrid.active := false 

   release object qSaveGrid 

return 

 

function form_onOpen 

   // get information for grid: 

   if file( "SaveGrid.dbf" ) 

      // create query: 

      qSaveGrid = new query() 

      qSaveGrid.sql = "select * from SaveGrid" 

      qSaveGrid.active := true 

      // loop through the table and set grid's Columns: 

      i=0 

      do while not qSaveGrid.rowset.endOfSet 

         i++ 

         // set the datalink: 

         cCommand = 'form.grid1.columns['+i+'].dataLink = form.'+; 

         qSaveGrid.rowset.fields["QueryName"].value.rightTrim( +'.rowset.fields["'+;

         qSaveGrid.rowset.fields["FieldName"].value.rightTrim()+'"]' 

         &cCommand. 

         // Column header: 

         form.grid1.columns[i].headingControl.value := ; 

         qSaveGrid.rowset.fields["FieldName"].value.rightTrim() 

         // next row 

         qSaveGrid.rowset.next() 

      enddo 

      // cleanup 

      qSaveGrid.active := false 

      release object qSaveGrid 

   endif 

return 

 

endclass