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

Selected record in a grid

Status
Not open for further replies.

MontyItaly

Programmer
Feb 5, 2013
23
IT
Sorry for my lacking english.
When I enter in a form with a grid, I with that the record selected not is the first bat same row down.
Exemple
----------------------- |
| Row1 |
| Row2 |
| Row3 record selected |
| Row4 |
| ........ |
------------------------|
Thank
Paolo
 
Hi Paolo,

Welcome to the forum.

To select a row in a grid, you only have to select the corresponding record in the underlying table.

So, if the third row of the grid corresponds to Record Number 3 in the table, you just have to go to that record:

Code:
GO 3 IN MyTable

You might then also need to set focus on the grid in order to see the selected row:

Code:
THISFORM.Grid1.SetFocus

I hope this makes sense.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
OK but the Record Number 3 in the table is displayed first in the grid, 1 and 2 is up, invisible.
I wish that in the grid is visible record 1 e record 2 not selected, record 3 selected ecc.....

 
Image1.jpg


An image is better...

Paolo
 
Code:
with thisform.grid1
   .setfocus()
   do while grid.relativerow<>3
      if grid.relativerow<3
         grid.doscroll(0) && scroll up (^)
      else
         grid.doscroll(1) && scroll down (v)
      enddo
   enddo
endwith

Use with caution: If you don't have 2 records before the active record, you'll never get out of the loop.
My recommendation: Live with the current row being the top one in a grid.

Bye, Olaf.
 
Seems like you need to have your data in order.

so, if you have an index on "unita di misura" field,

do this:
select YourTable
Set order to YourIndexTag

go to the record: either by seeking the value
example:

seek 'ccc'
thisform.YourGridname.Refresh()

if you do not have an index tag,
you can do a select statement, like this.

thisform.Yourgrid.RecordSource = ""
Select YourField from YourTable Order by YourField into cursor SomeCursor

locate for YourField = 'ccc'
if not found()
locate && go back top
endif
thisform.YourGrid.RecordSource = "SomeCursor"
thisform.YourGrid.Refresh()


Ali Koumaiha
TeknoSoft Inc.
Michigan
 
The code I showed (using GO 3... ) was just an example. The point is that you have to select the record (in the table), not the row (in the grid).

So, if the table has a field named Unita, and you want to select the row that shows "ccc" (like in your screen shot), then you can do this:

Code:
SELECT MyTable
LOCATE FOR Unita = "ccc"
THISFORM.Grid1.SetFocus

The exact method of selecting the record isn't important. The point is that whatever record you select, that will match the row that's selected in the grid.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Thanks to all, optimal solutions
OlafDoschke good solution, very dangerous near bottom file, but I work far from the bottom.

Paolo
 
Dangerous also in record 1 and 2. Testing recno() won't tell if you get into trouble, though, because in a different order any records can be the top or buttom records, for which the grid couldn't scroll further and might never reach relativerow 3 or whatever value.

What you could do in any situation is make the record relativerow 1 by LOCATE or SEEK, then scroll up twice, simply by
Code:
with thisform.grid1
   .setfocus()
   .doscroll(0)
   .doscroll(0)
Endwith
and in the normal case the outcome is the found row in relativerow 3 and if there are not enough records to scroll this still ends.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top