The dotted grid lines are drawn on the main tick marks like the axis labels.
I made an example showing how to get what you are looking for.
customgrid.C :
void drawgrid()
{
double xs = 0.2; // grid step along X
double ys = 0.2; // grid step along Y
double xmin,ymin,xmax,ymax;
gPad->GetRangeAxis(xmin,ymin,xmax,ymax);
auto aline = new TLine();
aline->SetLineStyle(3);
for (double yg = ymin+ys; yg < ymax; yg=yg+ys) aline->PaintLine(xmin,yg,xmax,yg);
for (double xg = xmin+xs; xg < xmax; xg=xg+xs) aline->PaintLine(xg,ymin,xg,ymax);
}
void customgrid()
{
gStyle->SetOptStat(0);
auto h = new TH2D("h","",10,0,10,10,0,10);
h->Draw();
h->GetXaxis()->SetRangeUser(2,8);
gPad->Update();
auto grid = new TExec("grid","drawgrid()");
grid->Draw();
}