I had no problems creating a grid like in the figure, but I can't create sharp edges at the end of the grid:
How can I do this?
If you are referring to the borders of your shape, the problem seems to be your brush. You are using a brush that is not "regular" like the lines inside the grid.
Just select your shape, and go to Brushes (if you can't seem, Window > Brushes
). Change your stroke to a brush that has the same weight everywhere. The first one in the list should be the 1px solid default.