|How to put grid on the form programmatically|
|Summary:||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).|
|Rating:|| Rate this entry|
|Vlad Grynchyshyn |
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.
&& create grid
&& 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.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.
&& define properties of combobox in column
.ColumnCount = 2
.ColumnWidths = 30,50
&& Don't forget to show grid ;)
.Visible = .T.
&& unlock screen if locked here
thisform.LockScreen = m.llNotLockedHere
More entries from this member
- 256-color icons in EXE application compiled by VFP May 28, 2002
VFP does not support 256-color icons as project's icon (icon displayed for EXE in Windows Explorer). But 16-color icons usually looks ugly and too simplified.
Here is an approach that allows organize 256-color icons for VFP EXE application.
The approach is based on use of Resource Hacker application. You can download it from following URL:
(UPDATED: original link does not work for some reason. Let me know if this one also does not ...
- Force positioning of text cursor by right mouse click in EditBox and RTF ActiveX controls February 8, 2002
There is a situation often when we need some way to position a text cursor in the text of Editbox or RTF ActiveX controlwhen right click on the control. For example, we want to display a shortcut menu on right click. In menu, user select an option that insert some text into the control on place of current position. However, right click on the control does not change the text cursor position, that confuse users. It would be good to right click, cursor moves to the clicked point and then menu disp...
- Grid reconstruction and how to avoid it March 16, 2001
Have you ever found a situation when your grid don't want to behave as you directed in design time? Custom controls in columns lost? Code of columns, headers or controls event not running? Read this FAQ then.
Grid reconstruction behavior is a complete removing of all grid's controls and columns and creating them again using default VFP controls. This cause lose of all methods, property settings and objects in columns and all column objects, column control to display and edit data is reset to ...
- How to assign code to event in run-time? September 21, 2001
In VFP there are no direct way to assign code to the method or event in run-time mode. However, some workaround is possible with use of additional, 'hook' class. The sample below describes the approach. It is possible to make it generic. The approach is very useful to catch, for example, events of the _Screen form.
- How to display Tool Tips for different parts of the Grid? February 1, 2002
In VFP6 Grid value of the ToolTipText property of controls is displayed only for the entire grid or for current control when mouse is over it. Becuase there are a lot of parts in the grid, it is often useful to display tool tip for different parts separately. Unfortunately, VFP interprets grid as a single control, displayed tool tip appearance for grid is not changed immediately even when ToolTipText property is changed, until mouse is moved out of the grid and returned into the grid into requir...
- How to freeze (lock) a column in the grid without use of split bar February 8, 2002
The approach is based on a simple principle. LeftColumn property of the grid represents a column that is currently the leftmost visible column in the grid with current horizontal scrolling. As we scroll the grid horizontally, this property changes. We can assign this number to the ColumnOrder property of the column we want to hold and that’s all:
this.Columns(1).ColumnOrder = this.LeftColumn
You can use this in the Scrolled and AfterRowColChange events of the grid. However, there is a...
- How to get form caption font properties October 6, 2000
Windows form caption properties stored in the user preferences. You can change them using Windows Control Panel\Display\Appearance. To get these parameters programmatically in VFP, you can use following routine as a sample.
- How to make a collection property of VFP object February 6, 2001
After some investigation, I managed to make a collection-like property of object. The following is a simple sample of how to organize collection-like access (rough, need to improve it to work as real collection). It is just to show an approach.
- How to make a grid appear like a list similar to list box June 20, 2001
This article describes how to configure grid to appear as a list similar to the list box or the drop-down list of combo box. It is for appearance only. All other things are as usual for any grid.
This is useful for case when need to replace list box by a grid to display more records, because listbox is slow when displaying 1000+ rows in listbox.
- How to make SQL Pass-Through cursor updatable April 9, 2002
The SQLEXEC() function in VFP allows downloading of data from any data source through ODBC. Unlike remote views, SQL Pass-Through approach does not provide default (automatic) settings in the returned cursor to update data on the server after changes in the cursor at the client side. Anyway, it is possible to customize required properties to organize updating.
First of all, it is recommended to read chapters about SQL Pass Through commands in the MSDN, specially, chapters that explain propert...
- How to organize SUM() for character field in SQL Server SELECT query September 4, 2001
There is a little problem for query to get something like a sum() for a character field to concatenate character values from all records in the group. Here is described solution.
The approach below have an limitation to max 30 records in group for concatenation. Test it if it will work for more records in group or there should be another limitation on your server, becaus ethis number is depended on SQL Server capabilities and length of the character field being concatenated.
- How to refresh grid properly after sorting January 26, 2001
I have seen many questions about sorting of a grid by clicking on its header and proper refreshing of grid after sorting. There is a reliable way to sort (index) grid cursor and properly refresh the grid without change of the record number AND with very good looking. I made this in my grid class and tested it already 2 years so far.
- How to work with Image field type on SQL Server (binary data)? January 11, 2001
Text field on SQL Serevr is not good to store binary data because it is not reliable and often SQL Server corrupts binary data in text field. varbinary type could be too short.
There are certain difficulties to use image field on SQL Server to store and read binary data in VFP. Image field on SQL Server mapped to general field in VFP by default, causing some difficulties when using SQL Pass Through functions. The workaround idea is to try to use memo field to update data on SQL server.
- SPT cursor fetching and progress displaying for SPT cursor downloading February 5, 2002
SQL Pass Through commands often can take a lot of time for running and returning the result set to the client. It is often usefult to show a progress bar in case of such lengthy process. Also, FetchAsNeeded option, available for views, could be very useful for SPT cursors as well. However, how we can open SPT cursor and set FetchAsNeeded option for it?
Here is a sample code for opening the cursor in "FetchAsNeeded" mode, as well as a description of approach for organizing the progress bar for...