ArcGIS Pro 3.7 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / GridOptionsEnvironment Class / SetGridOptions Method
The map on which the grid options will be applied
The grid options to apply
Example

In This Topic
    SetGridOptions Method (GridOptionsEnvironment)
    In This Topic
    Set the grid options for the given map.This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    Public Sub SetGridOptions( _
       ByVal map As Map, _
       ByVal gridOptions As GridOptions _
    ) 
    public void SetGridOptions( 
       Map map,
       GridOptions gridOptions
    )

    Parameters

    map
    The map on which the grid options will be applied
    gridOptions
    The grid options to apply
    Exceptions
    ExceptionDescription
    map cannot be null
    gridOptions cannot be null
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Example
    Get and Set Grid Options for a Map
    {
        //assume
        //var map = MapView.Active.Map;
    
        QueuedTask.Run(() =>
        {
            //If grid options have not been set for the map, then default grid options are added
            //for the map and returned otherwise the existing gridoptions are returned.
            //NOTE: Grid options are by value meaning a copy is returned. Any changes made to the
            //grid options need to be -set- back on the map using SetGridOptions for the changes to persist.
            var gridOptions = GridOptionsEnvironment.Instance.GetGridOptions(map);
            //change some properties
            gridOptions.RotateWithMap = !gridOptions.RotateWithMap;
            gridOptions.GridLineColor = ColorFactory.Instance.RedRGB;
            gridOptions.GridLineColor = ColorFactory.Instance.BlueRGB;
            //To apply changes, the gridoptions must be set back on the map
            GridOptionsEnvironment.Instance.SetGridOptions(map, gridOptions);
        });
    }
    Set Grid Origin for Grid Options for a Map
    {
        //assume
        //var map = MapView.Active.Map;
        var center_pt = MapView.Active.Extent.Center;
        var width = MapView.Active.Extent.Width;
        var height = MapView.Active.Extent.Height;
        var qtr_width = width * 0.25;
        var qtr_ht = height * 0.25;
    
        QueuedTask.Run(() =>
        {
            //If grid options have not been set for the map, then default grid options are added
            //for the map and returned otherwise the existing gridoptions are returned.
            //NOTE: Grid options are by value meaning a copy is returned. Any changes made to the
            //grid options need to be -set- back on the map using SetGridOptions for the changes to persist.
            var gridOptions = GridOptionsEnvironment.Instance.GetGridOptions(map);
    
            //Set the grid origin
            var x1 = center_pt.X + qtr_width;
            var y1 = center_pt.Y + qtr_ht;
            gridOptions.SetGridOrigin(new Coordinate2D(x1, y1));
            //or use Coordinate3D
            gridOptions.SetGridOrigin(new Coordinate3D(x1, y1, 0));
    
            //To apply changes, the gridoptions must be set back on the map
            GridOptionsEnvironment.Instance.SetGridOptions(map, gridOptions);
        });
    }
    Reset the Grid Options for a Map back to Defaults
    {
        QueuedTask.Run(() =>
        {
            if (GridOptionsEnvironment.Instance.HasGridOptions(map))
            {
                //set back to defaults - will overwrite existing
                GridOptionsEnvironment.Instance.SetGridOptions(map, 
                    GridOptionsEnvironment.Instance.DefaultGridOptions);
            }
        });
    }
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.7 or higher.
    See Also