Diagram for .NET / User's Guide / WinForm Components / Grid

In This Topic
    Grid
    In This Topic
    The grid is a view component, which renders a grid behind all document foreground content (i.e. in the View Background Paint Pass). It is represented by an instance of the NGrid class, a reference to which can be obtained from the Grid property of the NView
    C#
    Copy Code
    // get the grid
    NGrid grid = view.Grid;
    
    Visual Basic
    Copy Code
    ' get the grid
    Dim grid as NGrid = view.Grid
    
     Grid Appearance

    The grid can be displayed in several styles. The grid style is controlled by the GridStyle property, which can accept one of the following values:

    • MajorLines - draws a grid of major lines
    • MajorDots - draws a grid of major dots
    • MajorMinorLines - draws a grid of major-minor lines
    • Interlaced - horizontal and vertical stripes are displayed on each odd row and col
    • InterlacedHorizontally - horizontal stripes are displayed on each odd row
    • InterlacedVertically - vertical stripes are displayed on each odd col

    Depending on the grid style, different self-explanatory properties of the grid are used to control the appearance of the rendered strokes and filled areas. The following example makes an interlaced grid with semi - transparent yellow horizontal stripes, blue vertical stripes and red major lines:

    C#
    Copy Code
    // show the grid
    grid.Visible = true;
    
    // change its style
    grid.GridStyle = GridStyle.Interlaced;
    
    // change its appearance
    grid.HorizontalStripesFillStyle = new NColorFillStyle(Color.FromArgb(50, Color.Yellow));
    grid.VerticalStripesFillStyle = new NColorFillStyle(Color.FromArgb(50, Color.Blue));
    grid.MajorLinesStrokeStyle = new NStrokeStyle(1, Color.Red);
    
    Visual Basic
    Copy Code
    ' show the grid
    grid.Visible = True
    
    ' change its style
    grid.GridStyle = GridStyle.Interlaced
    
    ' change its appearance
    grid.HorizontalStripesFillStyle = New NColorFillStyle(Color.FromArgb(50, Color.Yellow))
    grid.VerticalStripesFillStyle = New NColorFillStyle(Color.FromArgb(50, Color.Blue))
    grid.MajorLinesStrokeStyle = New NStrokeStyle(1, Color.Red)
    

    By default the grid is only displayed within the document bounds - i.e. the grid is clipped with the document bounds. You can instruct it to occupy the entire viewport, by setting the ClipWithDocumentBounds property to false:

    C#
    Copy Code
    // display the grid in the entire viewport
    grid.ClipWithDocumentBounds = false;
    
    Visual Basic
    Copy Code
    ' display the grid in the entire viewport
    grid.ClipWithDocumentBounds = False
    
     Origin and Cell Size

    The point from which grid lines originate is called origin and is controlled by the Origin property. It is specified in the grid measurement unit. The origin and useful information about it, can optionally be displayed by setting the ShowOrigin property to true:

    C#
    Copy Code
    // show the grid origin
    grid.ShowOrigin = true;
    
    Visual Basic
    Copy Code
    ' show the grid origin
    grid.ShowOrigin = True
    

    The size of the of the grid cells can be determined in several modes. The cell size mode is controlled by the CellSizeMode property, which can accept one of the following values:

    • Fine - the grid automatically chooses a relatively small step based on the current view scale (twice smaller than normal)
    • Normal - the grid automatically chooses a relatively normal step based on the current view scale.
    • Coarse - the grid automatically chooses a relatively large step based on the current view scale (twice larger than normal) 
    • Fixed - the grid cell size is constant and is specified by the FixedCellSize property.

    The following code makes a fixed grid with cell size (50, 50) in the current grid measurement unit.

    C#
    Copy Code
    // change the cell size mode to fixed
    grid.CellSizeMode = AutoStepMode.Fixed;
    // set the fixed cell size
    grid.FixedCellSize = new NSizeF(50, 50);
    
    Visual Basic
    Copy Code
    ' change the cell size mode to fixed
    grid.CellSizeMode = AutoStepMode.Fixed
    ' set the fixed cell size
    grid.FixedCellSize = New NSizeF(50, 50)
    
     Related Examples
    Windows Forms: Drawing View - Grid
    See Also