Custom Cell

Cell body

ReoGrid provides an interface that supports the creation of any customized cell.
A cell body inside a cell, must always fit the size of cell, even if it is a merged cell. And body can use the properties of cell, such as data, formula, styles, and other resources. And, it is highly recommended to use data of cell to render label or anything else as body's text.



Interface of Cell body

A custom cell body must inherit from CellBody or implement from ICellBody. The CellBody class provides the following methods that can be overriden from user class:
public class CellBody : ICellBody
  public ReoGridCell Cell { get; set; }

  public virtual void OnSetup() { }

  public virtual Rectangle Bounds { get; set; }
  public virtual void OnBoundsChanged() { }

  public virtual void OnMouseDown(RGCellMouseEventArgs e) { }
  public virtual void OnMouseMove(RGCellMouseEventArgs e) { }
  public virtual void OnMouseUp(RGCellMouseEventArgs e) { }
  public virtual void OnMouseEnter(RGCellMouseEventArgs e) { }
  public virtual void OnMouseLeave(RGCellMouseEventArgs e) { }
  public virtual void OnMouseWheel(RGCellMouseEventArgs e) { }

  public virtual void OnKeyDown(KeyEventArgs e) { }
  public virtual void OnKeyUp(KeyEventArgs e) { }

  public virtual void OnPaint(RGDrawingContext dc) { }

  public virtual bool OnStartEdit() { return true; }
  public virtual object OnEndEdit(object data) { return data; }
  public virtual object OnPaste(object data) { return data; }
  public virtual void OnGotFocus() { }
  public virtual void OnLostFocus() { }

  public virtual object OnSetData(object data) { return data; }
  public virtual object OnSetText(string text) { return text; }

  public virtual object GetData() { return null; }

Create own cell body

Make class inheriting from CellBody, and add its instance into grid control.
private class MyCellBody : CellBody
  public MyCellBody() {

grid["C3"] = new MyCellBody();

Owner Drawing

By overriding the OnPaint method to draw anything in cell:
private class MyCellBody : CellBody
  public override void OnPaint(RGDrawingContext dc)
    // draw an ellipse inside cell
    dc.Graphics.DrawEllipse(Pens.Blue, base.Bounds);
Bounds property is the bounds of cell body that is managed by control automatically.
To recall core rendering module to render original cell text and background, the following methods which are provided by DCDrawingContext could be used:
dc.DrawCellText();         // recall to draw cell text
dc.DrawCellBackground();   // recall to draw cell background

Disable cell editing

By overriding OnStartEdit method and returning a false value to notify the control that editing operation should be aborted.
private class MyCellBody : CellBody
  public override bool OnStartEdit()
    return false;

Handle mouse and keyboard events

By overriding such as OnKeyDown and OnMouseDown methods to handle the keyboard and mouse events.
public override bool OnMouseDown(RGCellMouseEventArgs e)
  // translate cursor position to percent
  int value = (int)Math.Round(x * 100f / (Bounds.Width));

  // update cell's data, but do not set the data to cell directly, call methods of control instead
  grid.SetCellData(e.CellPosition, value);

Data bind by using formula

It is possible to bind two cell's data by using formula:
grid["C7"] = "=E7";

Next: Iterate Cells

Last edited Feb 7, 2014 at 6:52 AM by unvell, version 23