V4 V5

Borders live in ws.Borders (a BorderTable), separately from cell values. Only cells that carry a border appear in it, so the cost is proportional to how many borders there are, not to the sheet’s dimensions.

V4’s BorderPositions bit flags and presets (RangeBorderStyle.BlackSolid and friends) are gone.

A range outlined, with one edge replaced

Outlining a range

using unvell.ReoGrid.Core;

var edge = new BorderEdge(BorderLineStyle.Solid, 0xFF000000u, 1f);

ws.Borders.SetOutline(RangePosition.Parse("B2:D5"), edge);

BorderEdge is an immutable value holding a line style, a color (uint ARGB) and a width.

Line style (BorderLineStyle)
Noneno border
Solidsolid
Dasheddashed
Dotteddotted
Doubledouble
Thickthick

Setting one edge at a time

var thick = new BorderEdge(BorderLineStyle.Thick, 0xFF0066CCu, 2f);

ws.Borders.SetEdge(1, 1, BorderSide.Bottom, thick);
ws.Borders.SetEdge(1, 1, BorderSide.Right, BorderEdge.None);   // clear it

BorderEdge bottom = ws.Borders.GetEdge(1, 1, BorderSide.Bottom);
if (!bottom.IsNone)
	Console.WriteLine(bottom.Style);

BorderSide is Top / Right / Bottom / Left. To remove a border, set BorderEdge.None.

All four edges of a cell

CellBorders b = ws.Borders.Get(1, 1);

if (!b.IsEmpty)
	Console.WriteLine(b[BorderSide.Top].Width);

// make a new value with one edge replaced (CellBorders is immutable)
ws.Borders.Set(1, 1, b.With(BorderSide.Left, BorderEdge.None));

CellBorders is an immutable value holding all four edges. With(side, edge) returns a new value with one edge replaced.

Inner borders

The only range-wide API today is the outline (SetOutline). Inner borders are set in a loop.

// for inner horizontal lines, set the edge between each pair of rows
for (int r = range.Row; r < range.EndRow; r++)
	for (int c = range.Col; c <= range.EndCol; c++)
		ws.Borders.SetEdge(r, c, BorderSide.Bottom, edge);

For the current selection, the control has helpers.

  • control.SetSelectionOutline(color, width) — the outline
  • control.SetSelectionAllBorders(color, width) — every edge
  • control.ClearSelectionBorders()

Listing and clearing

int count = ws.Borders.Count;      // how many cells carry a border (unrelated to the sheet's dimensions)

foreach (var (row, col, borders) in ws.Borders.Entries())
	Console.WriteLine($"{row},{col}: {borders}");

ws.Borders.Clear();

Insert and delete

Borders follow row and column insertion and deletion automatically.

Was this article helpful?