After Revit 2018, the method for obtaining Grid Reference has been modified. You need to use the following code to retrieve it:

 foreach (Grid grid in grids)
            {
                var reference = new Reference(grid);
                gridReferences.Append(reference);
            }

Previously, you needed to get the Grid’s Curve.Reference to create dimensions:

gridReferences.Append(grid.Curve.Reference);

The change should be made to obtain Grid References more safely and conveniently. For details on the official response regarding this matter, please refer to the article below: Invalid Number of References

Image Description