Documents

NextGrid & NextDBGrid

NextGrid

NextDBGrid

NextInspector

NextSheet

NextCollection

Misc

Labs

NextGrid .NET

Custom Drawing in NextGrid dot NET

Related Articles:

Info
  • Skill:
  • Version: 1.0.0
  • Author: Bojan Nikolic
  • Created: 2010-12-30
  • Updated: 2011-02-06
How cell will be painted depends from column type of Cell and from Cell's content. This drawings may be additionally adjusted, or completely overridden within CustomDrawCell event.

1. How cell is painted



First background color is painted. It is painted in Cell's Color (BackColor property). If Cell is selected, it is filled with SelectionColor color. Then, content of the Cell is painted.

2. Set column for custom draw



Cell is not drawn in custom mode by default. This is controled by DrawingOptions property of Column:



Table 1: DrawingOptions
Name Meaning
Normal Cells within this column are drawn without custom draw.
Custom Cell will be drawn as usuall, but custom code may be added as extra painting layer on top over existing drawing.
CustomOnly Cell must be drawn manualy, even a background
ContentOnly Only Cell's content is drawn without Background of Cell being drawn.
BackgroundOnly Background is drawn as first layer (may be selected or not), but additional drawing must be done manualy.


3. CustomDrawCell event



CustomDrawCell event occurs for all cells that are currently on screen. This event consists of parameters that can be used to identify cell which is currently painted, and status of this cell.

Table 2: CustomDrawCell event parameters
Name Meaning
e Object containing Graphics property.
x Determine column index of currently painting cell.
y Determine row index of currently painting cell.
CellRectangle Determine Rectangle of currently painting cell.
selected Determine selected status of currently painting cell.
focused Determine if currently painting cell is focused.


4. Example



Next example use Cell's Rectangle and shrink it by 2px on each side. Then, if cell is selected draw rectangle in red color. If cell is not selected it will be painted in green color.

To draw graphics elements into grid we need to use Graphics property of e parameter.

DrawingOptions of Column should be set to BackgroundOnly.

private void nextGrid1_CustomDrawCell(object sender, PaintEventArgs e, int x, int y, Rectangle CellRectangle, bool selected, bool focused)
{
    CellRectangle.Inflate(-2, -2);

    if (selected)
    {
        e.Graphics.FillRectangle(new SolidBrush(Color.Red), CellRectangle);
    }
    else
    {
        e.Graphics.FillRectangle(new SolidBrush(Color.Lime), CellRectangle);
    }
}

Was This Article Useful?

Only constructive comments, code contributions... will be publishes. Questions, non-official discussion will not be published.