On the left side, we are basically setting up the pattern to repeat. I'm using UVs here which for a simple quad go from 0 to 1, but you might prefer to use worldspace rather than UVs for your 3D geometry. Your math will have to change but the adjustments shouldn't be hard to figure out.
The constant 25 represents our number of rows and columns. We multiply that by 2π because that is the period of the cosine function we'll use later. We multiply our UVs by that, and then separate out our X and Y because we're using them for vertical and horizontal lines respectively. Then we apply cosine to those values. This gives us a repeating "wave" between 0 and 1.
But we don't want a wave, we want lines. That is what we use the step function for. Step is basically if x > edge: return 1; else: return 0
but optimized for shaders. I chose 0.9 as the edge. Changing that will change the line thickness.
Now we have a number, 0 or 1, that tells us whether to draw a line or not. We use that to mix the colors for each direction, and then combine the directions together with a color operation. I used darken here, because the grid color is black, but other operations will work better for other inputs/results. The two color constants don't necessarily need to be colors. It might make sense for the first one to be a texture, or a whole nother bit of shader for lighting/etc.
My output is color, because this is just a CanvasItem shader, but you would want to make a Spatial shader, and so your output would be albedo.
Anyways, this shader probably won't work perfectly out of the box for you. You'll need to make adjustments for your situation if you want to use it and have it look right.