Say you want a 5x5 grid of images;
Code:
SELECT * FROM images WHERE id<>3 LIMIT 20
LIMIT may be TOP or something in M$SQL...

but I can't remeber
Right anyway,
each image should have a link to whereever - but - tag on the end of it something like this
(obviously replacing 3 with the value from the id column for the current row).
whatever page the user links to needs to keep that paramenter in the querystring (asp)/get variables (php) for when the user navigates back to the image grid page.
Now, in the query from eariler you need to change 3 to the value of that parmeter.
Summary (psuedo code)
Code:
check for img_id parameter, if not found set to -1
get a record set containing all records except any with an id=to img_id paremeter (limiting to 20 records)
display the images (making sure the links have a img_id parameter in)
Hope that makes sense, it was typed in a rush