A sample project that snaps the ScratchCat sprite to a specified grid when dragged. #gridSize specifies the intervals to be snapped to. #margin represents how much dead space / padding you want to the sides of the screen (prevents snapping too close to the edge of the screen). Set this to 0 to allow ScratchCat to reach the edges of the screen. (It is likely best to run this with the project maximized or while not in the editor to avoid conflicts with editor sprite dragging.)
The #gridSize and #margin are tracked at the project level and the stage has a loop to ensure that changes to the sliders do not go out of range. The sprite block #locateOnGrid() will determine the nearest grid snap point for the current mouse position while dragging the sprite. This block uses the floor() function to handle grid sizes that do not divide evenly into the screen dimensions (e.g. a #gridSize of 31). You can alter the effective margins of the upper- and right-sides by changing this to using ceiling() in these cases. The #snapToGrid() block moves ScratchCat's sprite to their current grid location's screen coordinates. You might consider how you would modify this project to have the grid located within a specific area of the screen instead of the entire screen. This project is an elaborate illustration of a relatively simple solution to snapping a sprite to a grid, which you can find on the wiki here (be careful not to overuse forever blocks, however): https://en.scratch-wiki.info/wiki/How_to_Snap_to_a_Grid