Similar to grids and reference planes grids are their own reference.
Create New Reference with grid as the argument in the constructor.
Below assumes you are in elevation or section.
Public Function TObj13(ByVal commandData As Autodesk.Revit.UI.ExternalCommandData, _
ByRef message As String, ByVal elements As Autodesk.Revit.DB.ElementSet) As Result
Dim IntApp As UIApplication = commandData.Application
Dim UIDoc As UIDocument = IntApp.ActiveUIDocument
If UIDoc Is Nothing Then Return Result.Cancelled Else
Dim IntDoc As Document = UIDoc.Document
Dim FEC As New FilteredElementCollector(IntDoc, IntDoc.ActiveView.Id)
Dim ECF As New ElementClassFilter(GetType(Level))
Dim IDS As IList(Of ElementId) = FEC.WherePasses(ECF).WhereElementIsNotElementType.ToElementIds
Dim Rs As IEnumerable(Of Reference)
Rs = From x As ElementId In IDS
Let El As Element = IntDoc.GetElement(x)
Let R As Reference = New Reference(El)
Select R
Dim RArr As New ReferenceArray()
For Each item As Reference In Rs
RArr.Append(item)
Next
Using Tx As New Transaction(IntDoc, "Add dimensions to levels")
If Tx.Start = TransactionStatus.Started Then
IntDoc.Create.NewDimension(IntDoc.ActiveView, Line.CreateBound(XYZ.Zero, XYZ.BasisZ), RArr)
Tx.Commit()
End If
End Using
Return Result.Succeeded
End Function