Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to put grid on the form programmatically

Classes and Objects

How to put grid on the form programmatically

by  TomasDill  Posted    (Edited  )
Suggest Grid1 is a name of grid required to put onto the form, grid should contain 10 columns an display data from the MyAlias alias. Grid should contain a combo box in the second colunm with the name Combo1. Following sample routine will allow you to see a generic idea of how to put grid on the form programmatically and recreate it again when needed. You can make it as a custom method of the form and call it when needed (for example, thisform.MakeGrid or thisform.RefreshGrid). See also comments in the code.
Note that when you need grid in some container or page of page frame, replace all thisform reference in routine by appropriate reference to the container, for example thisform -> thisform.PageFrame1.Page2.MyGridContainer
Note also that you can use your own classes for grid and combobox, just change second parameter of the AddObject methods in the code to your class name.
Routine demonstrates also a technique of screen locking for smooth refresh and how to check if object already exists.


local llFirstRun, llNotLockedHere
m.llNotLockedHere = thisform.LockScreen
m.llFirstRun = .T. && indicate that grid created first time
&& so we will not use lockscreen to
&& speed up form initialization

* test if grid is already on the form
if PEMSTATUS(thisform, 'Grid1', 5)
m.llFirstRun = .F. && this is not a first run - form is visible
* remove old grid object from form
thisform.LockScreen = .T.
* DOEVENTS && required only for case when following
&& code fail to remove grid properly
thisform.Grid1.Visible = .F.
thisform.RemoveObject('Grid1')
endif

&& create grid
thisform.AddObject('Grid1','Grid')
with thisform.Grid1
&& assign main thing - data source
.RecordSource = "MyAlias"
.ColumnCount = 10

&& define control sources - what each column will display
.Column1.ControlSource = 'MyAlias.Field1'
.Column2.ControlSource = 'MyAlias.Field2'
........
.Column10.ControlSource = 'MyAlias.Field10'

&& define column widths
.Column1.Width = 100
.Column2.Width = 50
.......

&& define column header captions
.Column1.Header1.Caption = 'Field1Caption'
.Column2.Header1.Caption = 'Field2Caption'
.......

&& Sample of how to add custom object into column
.Column2.AddObject('Combo1','MyCombobBoxClass')
.Column2.Combo1.Visible = .T.
.Column2.CurrentControl = 'Combo1'
&& remove original (default) control
&& NOTE: for header class do not need to
&& remove default header, it is removed automatically
&& when you add new header to keep only one
&& header in column at a time
.Column2.Text1.Visible = .F.
.Column2.RemoveObject('Text1')
&& define properties of combobox in column
with .Column2.Combo1
.ColumnCount = 2
.ColumnWidths = 30,50
.......
endwith

&& Don't forget to show grid ;)
.Visible = .T.
endwith

&& unlock screen if locked here
if !m.llFirstRun
thisform.LockScreen = m.llNotLockedHere
endif


Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top