Compartir a través de


How to: Select Indexes at Run Time

You can provide the capability for users to organize records interactively when they click a column header in a grid on a form. For example, suppose you want to permit application users to organize records in a grid on a form by clicking the column header they want to organize records by. To provide this capability, include the SET ORDER command to change the order of records in a form at run time.

To organize records interactively on a form

  1. Create a form with a Grid control.

  2. Set the grid's ColumnCount property to the number of fields you want to display in the grid.

  3. For the header of each column in the grid, insert code that includes the SET ORDER command in the header's Click event that performs the following tasks:

    • Sets the record order to an index key based on the column.

    • Refreshes the form.

For example, suppose you created a form based on the Customer table in the Visual FoxPro sample database, TestData, with a grid containing four columns: Company, Contact, Postal_Code, and Phone. The grid appears sorted alphabetically first because the records in that table were entered alphabetically. However, you can then make it possible for the user to view the grid ordered by contact name or postal code by inserting the code that uses the SET ORDER command in the Click event of each column header.

The following table describes sample code you can include in the Click event.

Code

Comment

SET ORDER TO Company

GO TOP

THISFORM.Refresh

In the Company header Click event code, reorder the grid using the Company index key and refresh the form to display records in order by company name.

SET ORDER TO Contact

GO TOP

THISFORM.Refresh

In the Contact header Click event code, reorder the grid by the Contact index key and refresh the form to display records in order by contact name.

SET ORDER TO

PostalCode

GO TOP

THISFORM.Refresh

In the Postal_Code header Click event code, reorder the grid by the PostalCode index key and refresh the form to display records in order by postal code.

In the example, when the form is first displayed, the grid appears in alphabetical order by company. When the user clicks the header of the Contact column, Visual FoxPro displays the records in the grid in alphabetical order by contact name. If the user clicks the Postal_Code column header, the grid is resorted and displayed in order by postal code.

See Also

Reference

SET ORDER Command

Grid Control

ColumnCount Property (Visual FoxPro)

Click Event

Other Resources

Working with Table Indexes