Create and use WorksheetRangeStyle
object to pass style items into cells or ranges, call SetRangeStyles
method of worksheet to set style object; ReoGrid always use an enumeration called PlainStyleFlag
to determine what styles should be handled.
Set Styles
There are several ways to set cell or range styles.
- Call worksheet methods
- Use Cell or Range instance
- Use Actions
- Use Executable Script
Call Worksheet Methods
Use method SetRangeStyles
from worksheet to set styles for 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 cell or range instance, then set theirs 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 by using action for the ability to undo, redo and repeat operations.
sheet.DoAction(new SetRangeStyleAction(RangePosition range, WorksheetRangeStyle styleSet));
Use Executable Script
When extension ReoGrid build is used, the script execution feature is available. By running script can change cell or range styles:
sheet.getCell(0, 0).style.backgroundColor = 'red';
Learn more about Script Execution.
Get Styles
There are following ways to get styles from cell or range:
- Call worksheet methods
- Use cell or range instance
- Use executable script
Call worksheet methods
Getting styles from cell or range by 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;
Use Executable Script
When extension ReoGrid build is used, the script execution feature is available. By running script can change cell or range styles:
var color = sheet.getCell(0, 0).style.backgroundColor;
Learn more about Script Execution.
Style Items
When call SetRangeStyles
method to set styles, the following style items are available for WorksheetRangeStyle
object:
Item |
Value of PlainStyleFlag |
Property Name | Type of property |
Background Color | BackColor | BackColor | Color or SolidColor |
Background Pattern Style | FillPatternStyle | FillPatternStyle | HatchStyles |
Background Pattern Color | FillPatternColor | FillPatternColor | Color or SolidColor |
Text Color | TextColor | TextColor | Color or SolidColor |
Font Name | FontName | FontName | string |
Font Size | FontSize | FontSize | float for Windows Form; double for WPF |
Font Style: Bold | FontStyleBold | Bold | bool |
Font Style: Italic | FontStyleItalic | Italic | bool |
Font Style: Underline | FontStyleUnderline | Underline | bool |
Font Style: Strikethrough | FontStyleStrikethrough | Strikethrough | bool |
Horizontal Alignment | HorizontalAlign | HAlign | ReoGridHorAlign |
Vertical Alignment | VerticalAlign | VAlign | ReoGridVerAlign |
Text Indent | Indent | Indent | ushort |
Wrap Mode | TextWrap | TextWrapMode | TextWrapMode |
Rotate Angle | RotateAngle | RotateAngle | int (-90°~90°) |
Padding of Cell Body | Padding | Padding | PaddingValue |
Background Color
Set background color to range B1:E5:
Don’t forget to set Flag
property, it determines which style item has the value to be applied.
sheet.SetRangeStyles("B1:E5", new WorksheetRangeStyle { Flag = PlainStyleFlag.FillColor, BackColor = Color.SkyBlue, });
Background Pattern Color
Set background pattern color to 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 });
Text color
Set text color to red:
sheet.SetRangeStyles("B2:B2", new WorksheetRangeStyle() { Flag = PlainStyleFlag.TextColor, TextColor = Color.Red, });
Text alignments
Text alignment can be specified on horizontal and vertical directions.
Set cell’s text horizontal alignment to Center
:
sheet.SetRangeStyles("A1:C3", new WorksheetRangeStyle { Flag = PlainStyleFlag.HorizontalAlign, HAlign = ReoGridHorAlign.Center, });
Text wrap
Set text wrap mode to WordWrap
: (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, });
Text Rotation
Set RotateAngle property of style object to rotate text inside cell:
var cell = sheet.Cells["A1"]; cell.Data = "Hello World"; cell.Style.RotateAngle = 90; // rotate 90°
Result:
Font
Change font name and size:
sheet.SetRangeStyles("B2:B2", new WorksheetRangeStyle() { Flag = PlainStyleFlag.FontSize | PlainStyleFlag.FontName, FontName = "Arial", FontSize = 20, });
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" };
Result:
Remove Style
Always remove style from range even for single cell.
Use worksheet method
By specifying PlainStyleFlag
to decide what styles should be removed. For example to remove all background styles from specified range:
sheet.RemoveRangeStyle(new RangePosition(2, 2, 3, 3), PlainStyleFlag.FillAll);
Use Actions
Remove all background styles from specified range:
sheet.DoAction(new RemoveRangeStyleAction(new RangePosition(2, 2, 3, 3), PlainStyleFlag.FillAll));
I am trying to do this:
// for cell
var cell = sheet.Cells[“B3”];
cell.Style.BackColor = Color.SkyBlue;
my code is:
var cell =shtSheet1.Cells{iSkidIndex, iTDIndex];
cell.Style.BackColor =
Completing the last line with Color.SkyBlue give an error that “The name ‘Color’ does not exist in the current context’
What am I doing wrong?? BTW, thanks for a great grid control. It is wonderful!!!
Thanks,
Brad Foster
Do you using Windows Form edition? If so you may have to import the
System.Drawing.DLL
and its namespace:using System.Drawing;
. Or you can useSolidColor
class instead ofColor
class.in C++/Cli , i have to set style twice.
is there a better way to set string at cell center (both horizontally & vertically)
WorksheetRangeStyle ^style = gcnew WorksheetRangeStyle();
style->Flag = PlainStyleFlag::VerticalAlign;
style->VAlign = ReoGridVerAlign::Middle;
reoGridControl1->CurrentWorksheet->SetRangeStyles(0, 0, reoGridControl1->CurrentWorksheet->RowCount, 4, style);
style->HAlign = ReoGridHorAlign::Center;
style->Flag = PlainStyleFlag::HorizontalAlign;
reoGridControl1->CurrentWorksheet->SetRangeStyles(0, 0, reoGridControl1->CurrentWorksheet->RowCount, 4, style);
Thanks & Regards
You can union the flag:
style->Flag = PlainStyleFlag::VerticalAlign | PlainStyleFlag::HorizontalAlign;
style->VAlign = ReoGridVerAlign::Middle;
style->HAlign = ReoGridHorAlign::Center;
reoGridControl1->CurrentWorksheet->SetRangeStyles(RangePosition.EntireRange, style);
Not sure RangePosition.EntireRange or RangePosition::EntireRange.
RangePosition::EntireRange worked. Thanks
One more question, can we set default row height of entire sheet using style?
I have to do this for every entry :
reoGridControl1->CurrentWorksheet->SetRowsHeight(0, reoGridControl1->CurrentWorksheet->RowCount, 35);
Thanks & Regards
No way to set default row height currently. You can submit a request at https://github.com/unvell/ReoGrid/issues, maybe someone will add this feature in next ReoGrid release.
Im trying to set background color, but PlainStyleFlag.FillColor flag does not sow up. Im working on a windows form. Thanks for your time