How to: Add and Remove Cell Borders
- 4 minutes to read
To access and specify cell borders, use the Borders object. To access this object in an individual cell, use the Cell object’s Formatting.Borders property, which is inherited form the Formatting interface. To change the borders of a range of cells, call the Range.BeginUpdateFormatting method for this range, and use the Borders property of the returned Formatting object to access and modify the Borders collection. Then call the Range.EndUpdateFormatting method to finalize the modification.
Each border of a cell is accessed via the corresponding property of the Borders object and represented by the Border object. This object provides the Border.LineStyle and Border.Color properties, allowing you to specify border line style and color, respectively. The BorderLineStyle enumerator lists the available line styles. The border color is defined by the Color value.
Use the following properties and methods to specify each particular border of a cell or cell range, or set several borders at once (for example, all outside, inside, vertical or horizontal borders).
- Borders.BottomBorder - sets the bottom border of a cell or range of cells.
- Borders.LeftBorder - sets the left border of a cell or range of cells.
- Borders.RightBorder - sets the right border of a cell or range of cells.
- Borders.TopBorder - sets the top border of a cell or range of cells.
- Borders.InsideHorizontalBorders - sets all horizontal borders within a range of cells.
- Borders.InsideVerticalBorders - sets all vertical borders within a range of cells.
- Borders.SetOutsideBorders - sets all outside borders of a cell or range of cells.
- Range.SetInsideBorders - sets all inside borders within a range of cells.
- Borders.SetAllBorders - sets all outside borders for a single cell or all outside and inside borders for a range of cells.
- Borders.RemoveBorders - removes all borders of a cell or range of cells.
To share the same cell border settings between multiple cells in one step, create or modify a style with the Formatting.Borders property specified as required, and assign this style to Range.Style for the desired cells.
This example demonstrates how to specify different borders for individual cells and ranges of cells by modifying the Borders object.
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-spreadsheetcontrol-api-e4655
Worksheet worksheet = workbook.Worksheets; // Set each particular border for the cell. Cell cellB2 = worksheet.Cells["B2"]; Borders cellB2Borders = cellB2.Borders; cellB2Borders.LeftBorder.LineStyle = BorderLineStyle.MediumDashDot; cellB2Borders.LeftBorder.Color = Color.Pink; cellB2Borders.TopBorder.LineStyle = BorderLineStyle.MediumDashDotDot; cellB2Borders.TopBorder.Color = Color.HotPink; cellB2Borders.RightBorder.LineStyle = BorderLineStyle.MediumDashed; cellB2Borders.RightBorder.Color = Color.DeepPink; cellB2Borders.BottomBorder.LineStyle = BorderLineStyle.Medium; cellB2Borders.BottomBorder.Color = Color.Red; // Set all borders for cells. Cell cellC4 = worksheet.Cells["C4"]; cellC4.Borders.SetAllBorders(Color.Orange, BorderLineStyle.MediumDashed); Cell cellD6 = worksheet.Cells["D6"]; cellD6.Borders.SetOutsideBorders(Color.Gold, BorderLineStyle.Double); // Set all borders for the range of cells in one step. Range range1 = worksheet.Range["B8:F13"]; range1.Borders.SetAllBorders(Color.Green, BorderLineStyle.Double); // Set all inside and outside borders separately for the range of cells. Range range2 = worksheet.Range["C15:F18"]; range2.SetInsideBorders(Color.SkyBlue, BorderLineStyle.MediumDashed); range2.Borders.SetOutsideBorders(Color.DeepSkyBlue, BorderLineStyle.Medium); // Set all horizontal and vertical borders separately for the range of cells. Range range3 = worksheet.Range["D21:F23"]; Formatting range3Formatting = range3.BeginUpdateFormatting(); Borders range3Borders = range3Formatting.Borders; range3Borders.InsideHorizontalBorders.LineStyle = BorderLineStyle.MediumDashDot; range3Borders.InsideHorizontalBorders.Color = Color.DarkBlue; range3Borders.InsideVerticalBorders.LineStyle = BorderLineStyle.MediumDashDotDot; range3Borders.InsideVerticalBorders.Color = Color.Blue; range3.EndUpdateFormatting(range3Formatting); // Set each particular border for the range of cell. Range range4 = worksheet.Range["E25:F26"]; Formatting range4Formatting = range4.BeginUpdateFormatting(); Borders range4Borders = range4Formatting.Borders; range4Borders.SetOutsideBorders(Color.Black, BorderLineStyle.Thick); range4Borders.LeftBorder.Color = Color.Violet; range4Borders.TopBorder.Color = Color.Violet; range4Borders.RightBorder.Color = Color.DarkViolet; range4Borders.BottomBorder.Color = Color.DarkViolet; range4.EndUpdateFormatting(range4Formatting);
The image below shows which borders are applied to cells after executing the code above.