Wiki Home

Grids How To Display Dynamic Data


Namespace: VB
Help, I'm writing a screen to reprint statements. I would like to give the users a field to enter last name and first names. The result will be displayed on a grid.
What property can I used to filter the data that the grid displays. Thanks.
Hugo95758@hotmail.com

Well, you could have ALL the data in the table the grid is bound to and filter the table to match the users entry, issue a locate and refresh the grid.

But, a better way is to create a parameterized view, bind the grid to it, populate the parameter variable with the name and requery the view.

What follows applies to Dynamic Grid Data, ie the cursorsource is created and or update AFTER the grid is created, where a view does not work. For example, I have been working with a form that has a 3 page pageframe. Each page has a grid which is based on user entries on the other grids. The data is updated through a series of SQL commands, resulting in the "grid losing it's mind." There are several solutions to this.

A. Do not close the cursor when updating it, but instead, zap it so it has zero records, then append the new data.

B. At design time, set up your gird like you want it to look, ie size the columns, headers etc. However, do not assign controlsource to the columns. Instead, leave them blank. At runtime, make the Record Source of the Grid the cursor. Ensure that the cursor has it's columns in the same order as you want them displayed. In this case, instead of zapping the Record Source as in the above solution, Assign the recordsource of the grid an empty value like this: Grid.RecordSource = "" This will prevent the grid from losing it's formating. Note however, that it still does lose the column controlsource, so in this solution, no control source is assigned and the grid is allowed to automatically assign the controlsource based on the cursor.

The downside to the automatic controlsource solution is that if the users re-orders the grid, and you then update the cursor with a new one, the column headers will no longer match the cursor as the grid is re-ordered, but the new cursor is not.

C. In this solution, you need to have the controlsource of each column specifically assigned and formatted. What you do is set up your grid at design time the way you want it including the grid recordsource, and column formatting and control source. When the form is run, it will display an empty grid even if the cursor has not yet been created. So the cursor is then created. To get the cursor to bind to the grid, use the following code:

loGrid = Thisform.grid1

* In practice, I find I have to hard code this value
lnColumns = loGrid.ColumnCount

* Save the current controlsource as they will be lost.
DIMENSION laControlSource[lnColumns]
FOR lnColumn = 1 TO lnColumns
	laControlSource[lnColumn] = loGrid.Columns[lnColumn].ControlSource
ENDFOR

* This saves the formating.
loGrid.RecordSource = ""


* Update your cursor here

loGrid.RecordSource = "MyCursor"

* Restore the controlsources
FOR lnColumn = 1 TO lnColumns
	loGrid.Columns[lnColumn].ControlSource = laControlSource[lnColumn]
ENDFOR

Thisform.Refresh()


The above solutions are based on the following:

When you assign the grid's recordsource at runtime, it will always lose the controlsources of the grid. Same is true at design time.

At runtime, you will also lose all formatting unless you assign the recordsource to the empty string BEFORE closing or updating the recordsource.

If you are using a view, you don't have to worry about any of this, as you can issue a Requery() which does not affect the grid.

Greg

Category Grid Basics
( Topic last updated: 2008.02.14 02:19:44 PM )