Microsoft KB Archive/140306

= How To Change a Grid's RecordSource Property Programmatically =

Article ID: 140306

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q140306



SUMMARY
This article shows by example how to change the RecordSource property of a grid programmatically. It also allows you to click the column heading to change the order of the table.



MORE INFORMATION
The method that does most of the work is SetGrid. It allows the user to select a table from an Open File dialog box. It sets the RecordSource property of the grid and the column headings to the field names in the table. It removes the any extra columns if the tables differ in the number of the fields they contain.

The order is set in the Click event of the header. Setting the order by clicking the column header can only be done programmatically because it requires a subclass of the header. Columns and headers can only be subclassed in code.

When the following code is run, the user will be presented with a dialog box to select a table. After table selection, the form appears. To change the table on which the grid is based, the user can click the button and they will again be presented with a dialog box to select a table.

Sample Code
* Begin Code PUBLIC oGridForm oGridForm = CREATEOBJECT("myForm") oGridForm.Show

DEFINE CLASS myForm AS FORM

Height = 350 ADD OBJECT myGrid AS myGrid WITH Top = 20, LEFT = 20,; Height = 200 ADD OBJECT myCommand as COMMANDBUTTON WITH Top = 250,; Left = 20, Height = 30, Caption = "Change Grid"

PROCEDURE myCommand.Click This.Parent.myGrid.SetGrid GO TOP ThisForm.ReFresh ENDPROC

ENDDEFINE

DEFINE CLASS myGrid AS GRID

ColumnCount = 0

PROCEDURE Init This.Parent.myGrid.SetGrid ThisForm.Refresh ENDPROC

PROCEDURE SetGrid ThisForm.Lockscreen = .T.          lcFileName = GETFILE("DBF","Select the Table:") USE (lcFileName) lcAlias = ALIAS This.RecordSource = "" This.RecordSource = lcAlias SELECT (lcAlias ) FOR i = 1 TO FCOUNT IF This.ColumnCount < i                 This.AddObject("column" + ALLTRIM(STR(i)),"MyColumn") ENDIF This.Columns(i).Visible = .t.              This.Columns(i).ControlSource = FIELD(i) This.Columns(i).Header1.Caption = FIELD(i) ENDFOR FOR i = FCOUNT + 1 TO This.ColumnCount This.RemoveObject("Column"+ALLTRIM(STR(i))) ENDFOR ThisForm.Caption = lcAlias ThisForm.Lockscreen = .F.       ENDPROC

ENDDEFINE

DEFINE CLASS myColumn as Column

ADD OBJECT Header1 as myHeader WITH Visible = .T.

ENDDEFINE

DEFINE CLASS myHeader AS HEADER

PROCEDURE Click FOR nCount = 1 TO 254 IF !EMPTY(TAG(nCount)) IF This.Caption = TAG(nCount) SET ORDER TO This.Caption EXIT ENDIF ELSE EXIT ENDIF ENDFOR This.Parent.Parent.ReFresh This.Parent.Parent.SetFocus ENDPROC

ENDDEFINE * End Code

Keywords: kbhowto kbcode KB140306

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.