Border

Borders can be set by programming, by calling APIs worksheet directly, also can be set by performing actions.

Border Styles

The following border styles are supported by current version of ReoGrid.

264

Border styles are defined as BorderLineStyle enumeration.

Border Positions

Borders must be set around a range, even the range contains only one cell. To specify border positions, use BorderPositions enumeration and its binary combined value.

266

Set borders

To set borders, use SetRangeBorders method:

var sheet = reoGridControl.CurrentWorksheet;

sheet.SetRangeBorders("B3:D7", BorderPositions.Outside,
  new RangeBorderStyle
  {
    Color = Graphics.SolidColor.Magenta,
    Style = BorderLineStyle.BoldSolid,
  });

18_2

Set borders by performing action

Setting borders by actions can be undone. For example, to set all dashed borders inside a range:

reoGridControl.DoAction(reoGridControl.CurrentWorksheet,
  new SetRangeBorderAction(new RangePosition(2, 1, 5, 3), 
    BorderPositions.All, 
    new RangeBorderStyle { 
      Color = Graphics.SolidColor.Red, 
      Style = BorderLineStyle.Dashed 
    })
);

19_2

Get borders

To get borders from a range, use GetRangeBorders method of worksheet.

RangeBorderInfoSet borderInfoSet = sheet.GetRangeBorders("A1:D5");

The method GetRangeBorders filters all same borders and returns only one style instance. Check the property NonUniformPos to see what positions of border in the range are not same.

Iterate Borders

To iterate over all borders in a specified range. use IterateBorders method of worksheet.

Define:

IterateBorders(RowOrColumn direction, RangePosition range, Func<int, int, int, RangeBorderStyle, bool> iterator);

where

  • direction determines the iterating mode:
    • RowOrColumn.Row – iterates over all horizontal borders
    • RowOrColumn.Column – iterates over all vertical borders
    • RowOrColumn.Both – iterates over all borders (both Row and Column)
  • range is the target range to be iterated
  • iterator is a callback function that is provided by user code to scan all borders returned from worksheet, which has following arguments:
    • int Row – the number of row where border exists
    • int Column – the number of column where border exists
    • int Span – how many borders exist with same styles from this position
    • RangeBorderStyle Border information – contains the color and style information of border
    • bool return value – return true to continue iterating; return false to abort

For example, iterate over all horizontal borders and markup cells as red background if the position of the cell has borders.

// set borders
worksheet.Ranges["C3:F10"].BorderInsideHorizontal = RangeBorderStyle.BlackSolid;

// iterate borders
worksheet.IterateBorders(RowOrColumn.Row, RangePosition.EntireRange, (r, c, span, border) =>
{
  worksheet.Cells[r, c].Style.BackColor = Color.LightCoral;
  return true;
});

Result:

277

Iterate over all vertical borders:

// set borders
worksheet.Ranges["C3:F10"].BorderInsideVertical = RangeBorderStyle.BlackSolid;

// iterate borders
worksheet.IterateBorders(RowOrColumn.Column, RangePosition.EntireRange, (r, c, span, border) =>
{
  worksheet.Cells[r, c].Style.BackColor = Color.LightCoral;
  return true;
});

Result:

278

Iterate over all borders, both Row and Column borders:

// set borders
worksheet.Ranges["C3:F10"].BorderOutside = RangeBorderStyle.BlackBoldSolid;
worksheet.Ranges["C3:F10"].BorderInsideHorizontal = RangeBorderStyle.BlackSolid;
worksheet.Ranges["C3:F10"].BorderInsideVertical = RangeBorderStyle.BlackDotted;

// iterate borders
worksheet.IterateBorders(RowOrColumn.Both, RangePosition.EntireRange, (r, c, span, border) =>
{
  worksheet.Cells[r, c].Style.BackColor = Color.LightCoral;
  return true;
});

Result:

279

See also: Iterate Cells

Remove borders

To remove borders from worksheet, use RemoveRangeBorders method.

sheet.RemoveRangeBorders(new RangePosition(2, 1, 5, 1), ReoGridBorderPos.All);

Setting border’s style to RangeBorderStyle.Empty is also possible to remove borders:

sheet.SetRangeBorders(2, 1, 5, 3, ReoGridBorderPos.All, RangeBorderStyle.Empty);

Remove borders by doing action

Removing borders by action can be undone, for example:

reoGridControl.DoAction(reoGridControl.CurrentWorksheet,
  new RemoveRangeBorderAction(new RangePosition(2, 1, 5, 1), ReoGridBorderPos.All));

To undo this removing operation:

reoGridControl.Undo();

Excel support

ReoGrid support reading and saving all borders with styles from or to Excel file.

Demo project

Example code about using borders is available in demo project.


Next: Style