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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Entering data into a grid. - vertical positioning

Status
Not open for further replies.

AndrewMozley

Programmer
Oct 15, 2005
621
GB

I am revisiting some old forms ! and am uncertain about how the data is positioned in a grid when new records may be being added to its recordsource.

There is a grid, grdmain which allows the user to enter data into its recordsource, cursor tdetail.

Intially there are two blank records in tdetail. Tdetail is positioned at its first record, so the active cell is on the top LH of the grid, corresponding to tdetail.tdesc (a 30 character description field). When the user keys “Desc1” on the screen and presses the Dnarrow key, several events occur, including grdmain.AfterRowColChange(). This checks whether we are now on the last record of tdetail. If we are, my code appends a record to tdetail, so that (if the user navigates down again) there is a record to go to.

This works fine, so the screen now has focus on line 2 of the visible grid, with the preceding line visible and a spare line to navigate to – if required. There are 7 visible lines (but there could be more - the form can be re-sized)

But after entering line 4, and adding a new record to cursor tdetail the grid scrolls up, so lines 2-4 are shown (with contents Desc1, Desc2 &c displayed. And the blank line is just below the current active row.

That is fine and the user can continue entering data, or scroll back up the grid to edit any of the preceding lines (the grid may scroll as necessary. I think that rhis is the standard behaviour for a grid - don’t believe that my grid class is causing this useful scrolling.

But sometimes the user might prefer (after entering each line and pressing DnArrow) that the grid should not scroll, so that he can see all the preceding lines - at least until there are too many to fit in the visible grid.

Is there a way that I can specify where the data is positioned vertically positioned on the grid (and possibly inhibit the automatic scrolling)? And how can I determine how may rows of the grid are visible on the screen?

Thank you.
 
I assume the form is resizable, so the row number can vary, otherwise you have control at design time how many rows you'll have.
You can compute the number of visible rows by grid.Rowheight and Grid.height, of course taking into account header and border lines.

If line 1 already scrolls up and moves out of view, your second blank line must already be causing this, not fitting into the grid area. And I think your users don't assume the line they are editing is only the second to last row.

Look at Grid.AllowAddNew. This will do the job without any code in RowColChange and you won't need one empty row for data entry/editing and another to be able to move down.



Chriss
 
Andrew,

In addition to what Chris has told you, you asked how to "specify where the data is positioned vertically positioned on the grid". You can use the RelativeRow property to find the relative position of the active row relative to the top of the grid. And then you can use DoScroll to scroll up or down one row at a time until the active row is where you want it.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thank you Chris. The form is indeed re-sizable - I should have made that clearer. Your suggestion of GridRowHeight was helpful.

However the form is being scrolled so that the earlier lines scroll off the top., leaving blank lines on the lower part of the grid empty, and the line with focus in the middle of the screen

Thank you Mike for your indication of a solution. I can indeed check RelativeRow, and compare it with the number of rows in the grid, which I can calculate on the lines you suggest. Then, in the AfterRowColChange() method, I can execute a number of DoScroll() commands.

That seems to do the trick. Still not sure why the grid has scrolled so that the line with focus is only just below the middle of the screen. That might seem reasonable enough, but if I have entered 9 lines into a grid which allows 12 lines, I would usually prefer all 9 lines to be visible, with just 3 blank rows of the grid at the bottom.
 
You're right about the scrolling. I never noticed it, but I saw randomly many empty lines appear when using PgDwn. Even when you scroll down the grid with the scroll bar thumb, it ends differently. Seems to me VFP scrolls full pages and depending on how many records are on the last 'page' you get differently many empty rows. When you then scroll up until the last record is the last visible row, you can scroll with PgUp and PgDwn and get back to that.

I'd still stop adding empty rows just to be able to have the down arrow working. AllowAddNew does that, and once you use that, when you get to the bottom the grid only scrolls down one row and the last visible row becomes the active editing row. If there is one further empty row the grid can't display 100% of it, that'll be empty. So ideally the grid height should grow in rowheight increments, but it's a minor issue.

To make use of AllowAddRow and also have an easy way to scroll to the end, so it shows in the last grid row, Mikes solution works. Maybe put that into an extra 'Go Bottom' button. Also account for the situation you have fewer records as visible grid rows, then the first row will always stay the grid top row. A while loop exiting when the relativerow is the number of rows would then lead to an endless loop.


Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top