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.

Namespace

using unvell.ReoGrid;

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

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

SetRangeStyles Overloads

SignatureDescription
SetRangeStyles(string addressOrName, WorksheetRangeStyle style)Set styles by address or named range
SetRangeStyles(int row, int col, int rows, int cols, WorksheetRangeStyle style)Set styles by row/col/size
SetRangeStyles(RangePosition range, WorksheetRangeStyle style)Set styles by range position

Use Cell or Range Instance

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

// 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("B1:E5", new WorksheetRangeStyle
{
    Flag = PlainStyleFlag.FillColor,
    BackColor = Color.SkyBlue,
}));

Get Styles

Call Worksheet Methods

// Get style from a single cell
WorksheetRangeStyle style = sheet.GetCellStyle("B3");
WorksheetRangeStyle style = sheet.GetCellStyle(new CellPosition("B3"));
WorksheetRangeStyle style = sheet.GetCellStyle(2, 1);

Use Cell or Range Instance

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

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

WorksheetRangeStyle

The WorksheetRangeStyle class holds the style properties to apply. The Flag property determines which style items are included.

Properties

PropertyPlainStyleFlag ValueTypeDescription
Flagโ€”PlainStyleFlagDetermines which styles are included in this object
BackColorBackColorSolidColorBackground color
FillPatternColorFillPatternColorSolidColorBackground pattern color (WinForms only)
FillPatternStyleFillPatternStyleHatchStylesBackground pattern style (WinForms only)
TextColorTextColorSolidColorText color (default: Black)
FontNameFontNamestringFont name
FontSizeFontSizefloatFont size (default: 10)
BoldFontStyleBoldboolBold font
ItalicFontStyleItalicboolItalic font
StrikethroughFontStyleStrikethroughboolStrikethrough
UnderlineFontStyleUnderlineboolUnderline
HAlignHorizontalAlignReoGridHorAlignHorizontal alignment
VAlignVerticalAlignReoGridVerAlignVertical alignment
TextWrapModeTextWrapTextWrapModeText wrap mode
IndentIndentushortText indent (0โ€“65535)
PaddingPaddingPaddingValueCell padding
RotationAngleRotationAnglefloatText rotation angle (โˆ’90ยฐ to 90ยฐ)

Methods

MethodDescription
WorksheetRangeStyle()Create empty style set
WorksheetRangeStyle(WorksheetRangeStyle source)Copy constructor
Clone(WorksheetRangeStyle source)Static: clone a style object
CopyFrom(WorksheetRangeStyle source)Copy styles from another object
HasStyle(PlainStyleFlag flag)Check if all specified flags are present
HasAny(PlainStyleFlag flag)Check if any specified flag is present

Note: Always set the Flag property โ€” it determines which style items will be applied. Style properties without the corresponding flag set are ignored.

PlainStyleFlag

The PlainStyleFlag enum (long, flags) specifies which style items to apply or remove.

Individual Flags

ValueHexDescription
None0x0No style
FontName0x1Font name
FontSize0x2Font size
FontStyleBold0x4Bold
FontStyleItalic0x8Italic
FontStyleStrikethrough0x10Strikethrough
FontStyleUnderline0x20Underline
TextColor0x40Text color
BackColor0x80Background color
HorizontalAlign0x2000Horizontal alignment
VerticalAlign0x4000Vertical alignment
FillPatternColor0x80000Background pattern color
FillPatternStyle0x100000Background pattern style
TextWrap0x200000Text wrap mode
Indent0x400000Indent
Padding0x800000Padding
RotationAngle0x1000000Text rotation angle

Composite Flags

ValueCombines
FontStyleAllFontStyleBold | FontStyleItalic | FontStyleStrikethrough | FontStyleUnderline
FontAllFontName | FontSize | FontStyleAll
AlignAllHorizontalAlign | VerticalAlign
FillPatternFillPatternColor | FillPatternStyle
BackAllBackColor | FillPattern
LayoutAllTextWrap | Padding | RotationAngle
AllFontAll | TextColor | BackAll | AlignAll | LayoutAll

Alignment Enums

ReoGridHorAlign

ValueDescription
GeneralAutomatic alignment (numbers right, text left)
LeftLeft-aligned
CenterCentered
RightRight-aligned

ReoGridVerAlign

ValueDescription
GeneralDefault alignment
TopTop-aligned
MiddleMiddle-aligned
BottomBottom-aligned

TextWrapMode

ValueDescription
NoWrapNo wrapping (default)
WordBreakNormal word break
BreakAllBreak at any character

Background Color

Set background color for the range B1:E5:

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

15_2_2

Background Pattern Color

Set background pattern color for the entire grid (WinForms only):

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

14

Text Color

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

15_2

Text Alignment

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 WordBreak (the default is NoWrap):

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 RotationAngle property to rotate text inside a cell (โˆ’90ยฐ to 90ยฐ):

var cell = sheet.Cells["A1"];
cell.Data = "Hello World";
cell.Style.RotationAngle = 90;

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:

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

sheet.SetRangeStyles("C2", new WorksheetRangeStyle
{
    Flag = PlainStyleFlag.FontStyleItalic,
    Italic = true,
});

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

437

Multiple Styles at Once

Combine multiple PlainStyleFlag values using bitwise OR to set several styles in one call:

sheet.SetRangeStyles("A1:D4", new WorksheetRangeStyle
{
    Flag = PlainStyleFlag.FillColor | PlainStyleFlag.TextColor
         | PlainStyleFlag.FontStyleBold | PlainStyleFlag.HorizontalAlign,
    BackColor = Color.LightYellow,
    TextColor = Color.DarkBlue,
    Bold = true,
    HAlign = ReoGridHorAlign.Center,
});

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:

sheet.RemoveRangeStyles(new RangePosition(2, 2, 3, 3), PlainStyleFlag.BackAll);

RemoveRangeStyles Overloads

SignatureDescription
RemoveRangeStyles(string addressOrName, PlainStyleFlag flags)Remove by address or named range
RemoveRangeStyles(RangePosition range, PlainStyleFlag flags)Remove by range position

Use Actions

sheet.DoAction(new RemoveRangeStyleAction(new RangePosition(2, 2, 3, 3), PlainStyleFlag.BackAll));

Root Style

The RootStyle property on the worksheet defines the default style for all cells:

sheet.RootStyle.FontName = "Consolas";
sheet.RootStyle.FontSize = 12;

Alternate Row Styles

Apply alternating row colors for improved readability:

sheet.AlternateNormalRowColor = SolidColor.White;
sheet.AlternateAccentRowColor = new SolidColor(240, 240, 250);
sheet.AlternateRowRange = new RangePosition("A1:F100");
sheet.AlternateRowStyleMode = AlternateRowStyleMode.ByVisibleRow;

AlternateRowStyleMode

ValueDescription
NoneNo alternate row styling
ByVisibleRowAlternate by visible rows (skips hidden rows)
ByPhysicalRowAlternate by physical row index

Events

EventEventArgsDescription
RangeStyleChangedRangeEventArgsRaised when styles are changed on a range

ReferenceCellStyle

The ReferenceCellStyle class is returned by cell.Style. Setting a property on this object automatically applies the style to the cell with the correct flag.

PropertyTypeDescription
BackColorSolidColorBackground color
TextColorSolidColorText color
FontNamestringFont name
FontSizefloatFont size
BoldboolBold
ItalicboolItalic
StrikethroughboolStrikethrough
UnderlineboolUnderline
HAlignReoGridHorAlignHorizontal alignment
VAlignReoGridVerAlignVertical alignment
TextWrapTextWrapModeText wrap mode
IndentushortText indent
PaddingPaddingValueCell padding
RotationAnglefloatText rotation angle

ReferenceRangeStyle

The ReferenceRangeStyle class is returned by range.Style. Setting a property applies the style to the entire range.

PropertyTypeDescription
BackColorSolidColorBackground color
TextColorSolidColorText color
FontNamestringFont name
FontSizefloatFont size
BoldboolBold
ItalicboolItalic
UnderlineboolUnderline
StrikethroughboolStrikethrough
HorizontalAlignReoGridHorAlignHorizontal alignment
VerticalAlignReoGridVerAlignVertical alignment
TextWrapTextWrapModeText wrap mode
PaddingPaddingValueCell padding
IndentushortText indent
Was this article helpful?