Create and use a WorksheetRangeStyle object to pass style items into cells or ranges. Call the SetRangeStyles method on the worksheet to apply the style object. ReoGrid uses an enumeration called PlainStyleFlag to determine which styles should be applied.

Set Styles

There are several ways to set cell or range styles:

  • Call worksheet methods
  • Use a Cell or Range instance
  • Use Actions

Call Worksheet Methods

Use the SetRangeStyles method on the worksheet to set styles for a cell or range:

sheet.SetRangeStyles("B1:E5", new WorksheetRangeStyle
{
  // style item flag
  Flag = PlainStyleFlag.FillColor,
  // style item
  BackColor = Color.SkyBlue,
});

Use Cell or Range instance

Get a cell or range instance, then set its Style property to change styles.

// for cell
var cell = sheet.Cells["B3"];
cell.Style.BackColor = Color.SkyBlue;

// for range
var range = sheet.Ranges["B1:C3"];
range.Style.BackColor = Color.LightYellow;

Use Actions

Set styles using an action to enable undo, redo, and repeat operations.

sheet.DoAction(new SetRangeStyleAction(RangePosition range, WorksheetRangeStyle styleSet));

Get Styles

The following methods can be used to get styles from a cell or range:

  • Call worksheet methods
  • Use a cell or range instance
  • Use executable script

Call worksheet methods

Getting styles from a cell or range using worksheet methods:

sheet.GetRangeStyle(RangePosition range);
sheet.GetCellStyle(CellPosition pos);

Use cell or range instance

// for cell
var cell = sheet.Cells["B3"];
var backColor = cell.Style.BackColor;

// for range
var range = sheet.Ranges["B1:C3"];
var backColor = range.Style.BackColor;

Style Items

When calling the SetRangeStyles method to set styles, the following style items are available on the WorksheetRangeStyle object:

ItemValue of PlainStyleFlagProperty NameType of property
Background ColorBackColorBackColorColor or SolidColor
Background Pattern StyleFillPatternStyleFillPatternStyleHatchStyles
Background Pattern ColorFillPatternColorFillPatternColorColor or SolidColor
Text ColorTextColorTextColorColor or SolidColor
Font NameFontNameFontNamestring
Font SizeFontSizeFontSizefloat for Windows Forms; double for WPF
Font Style: BoldFontStyleBoldBoldbool
Font Style: ItalicFontStyleItalicItalicbool
Font Style: UnderlineFontStyleUnderlineUnderlinebool
Font Style: StrikethroughFontStyleStrikethroughStrikethroughbool
Horizontal AlignmentHorizontalAlignHAlignReoGridHorAlign
Vertical AlignmentVerticalAlignVAlignReoGridVerAlign
Text IndentIndentIndentushort
Wrap ModeTextWrapTextWrapModeTextWrapMode
Rotate AngleRotateAngleRotateAngleint (-90° ~ 90°)
Padding of Cell BodyPaddingPaddingPaddingValue

Background Color

Set background color for the range B1:E5:

sheet.SetRangeStyles("B1:E5", new WorksheetRangeStyle
  {
    Flag = PlainStyleFlag.FillColor,
    BackColor = Color.SkyBlue,
  });

::info Don’t forget to set the Flag property — it determines which style item has the value to be applied. ::

15_2_2

Background Pattern Color

Set background pattern color for the entire grid:

sheet.SetRangeStyles(RangePosition.EntireRange,
  new unvell.ReoGrid.RangePositionStyle {
    Flag = PlainStyleFlag.FillPattern | PlainStyleFlag.FillColor,
    BackColor = Color.LightYellow,
    FillPatternColor = Color.SkyBlue,
    FillPatternStyle = System.Drawing.Drawing2D.HatchStyle.DiagonalBrick
  });

14

Text color

Set text color to red:

sheet.SetRangeStyles("B2:B2", new WorksheetRangeStyle()
{
  Flag = PlainStyleFlag.TextColor,
  TextColor = Color.Red,
});

15_2

Text alignments

Text alignment can be specified in both horizontal and vertical directions.

15_3

Set the cell’s horizontal text alignment to Center:

sheet.SetRangeStyles("A1:C3", new WorksheetRangeStyle
  {
    Flag = PlainStyleFlag.HorizontalAlign,
    HAlign = ReoGridHorAlign.Center,
  });

16_2

Text wrap

Set text wrap mode to WordWrap (the default is no-wrap):

sheet[1, 1] = "How many beers can you drink?";
sheet.SetRangeStyles("B2:B2", new WorksheetRangeStyle()
{
  Flag = PlainStyleFlag.TextWrap,
  TextWrapMode = TextWrapMode.WordBreak,
});

15_4

Text Rotation

Set the RotateAngle property of the style object to rotate text inside a cell:

var cell = sheet.Cells["A1"];
cell.Data = "Hello World";
cell.Style.RotateAngle = 90;  // rotate 90°

Result:

345

Font

Change font name and size:

sheet.SetRangeStyles("B2:B2", new WorksheetRangeStyle()
{
  Flag = PlainStyleFlag.FontSize | PlainStyleFlag.FontName,
  FontName = "Arial",
  FontSize = 20,
});

15_5

Change font styles:

// use method
sheet.SetRangeStyles("B2", new unvell.ReoGrid.WorksheetRangeStyle
{
  Flag = unvell.ReoGrid.PlainStyleFlag.FontStyleBold,
  Bold = true,
});

// use method
sheet.SetRangeStyles("C2", new unvell.ReoGrid.WorksheetRangeStyle
{
  Flag = unvell.ReoGrid.PlainStyleFlag.FontStyleItalic,
 Italic = true,
});

// use cell instances
sheet.Cells["B3"].Style.Underline = true;
sheet.Cells["C3"].Style.Strikethrough = true;

// set data
sheet["B2:C3"] =new object[] { "Bold", "Italic", "Underline", "Strikethrough" };

437

Remove Style

Always remove styles from a range, even for a single cell.

Use worksheet method

Specify PlainStyleFlag to determine which styles should be removed. For example, to remove all background styles from a specified range:

sheet.RemoveRangeStyle(new RangePosition(2, 2, 3, 3), PlainStyleFlag.FillAll);

Use Actions

Remove all background styles from a specified range:

sheet.DoAction(new RemoveRangeStyleAction(new RangePosition(2, 2, 3, 3), PlainStyleFlag.FillAll));
Was this article helpful?