# dotNet.ReoGrid 扩展库 - 边框设置

边框可以通过编程方式设置，直接调用工作表API，也可以通过执行操作来设置。

## 边框样式

当前版本的ReoGrid支持以下边框样式：

![264](../images/264.png)

边框样式定义为`BorderLineStyle`枚举。

## 边框位置

边框必须围绕一个区域设置，即使该区域只包含一个单元格。要指定边框位置，请使用`BorderPositions`枚举及其二进制组合值。

![266](../images/266.png)

## 设置边框

要在工作表中为特定区域应用边框，使用`SetRangeBorders`方法：

```aardio
import dotNet.ReoGrid;
var sheet1 = reoGridControl.CurrentWorksheet;
// 为指定区域外部应用自定义样式的边框
sheet1.SetRangeBorders("B3:D7", ReoGrid.BorderPositions.Outside, ReoGrid.RangeBorderStyle(
    Color = ReoGrid.Graphics.SolidColor.Magenta,
    Style = ReoGrid.BorderLineStyle.BoldSolid
));
```

![18_2](../images/18_2.png)

### 通过操作设置边框

要通过可撤销的操作在区域内应用边框，使用以下方法：

```aardio
reoGridControl.DoAction(reoGridControl.CurrentWorksheet, 
    ReoGrid.Actions.SetRangeBorderAction(
        ReoGrid.RangePosition(2, 1, 5, 3),
        ReoGrid.BorderPositions.All,
        ReoGrid.RangeBorderStyle({
            Color = ReoGrid.Graphics.SolidColor.Red,
            Style = ReoGrid.BorderLineStyle.Dashed
        })
    )
);
```

![19_2](../images/19_2.png)

## 获取边框信息

要获取工作表中特定区域应用的边框详细信息，使用`GetRangeBorders`方法：

```aardio
// 获取指定区域的边框信息
var borderInfoSet = sheet1.GetRangeBorders("A1:D5");
```

`GetRangeBorders`方法分析指定区域内的边框并返回一个`RangeBorderInfoSet`。该集合包含在整个区域内一致应用的边框样式实例。

要识别区域内样式不同的边框，检查`RangeBorderInfoSet`的`NonUniformPos`属性。该属性突出显示与主要样式不同的边框位置。

## 遍历边框

要枚举指定区域内的所有边框，使用工作表的`IterateBorders`方法。

### 方法定义

```cs
IterateBorders(RowOrColumn direction, RangePosition range, Func<int, int, int, RangeBorderStyle, bool> iterator);
```

### 参数

* `direction` - 指定迭代模式：
    * `RowOrColumn.Row` 迭代所有水平边框
    * `RowOrColumn.Column` 迭代所有垂直边框
    * `RowOrColumn.Both` 迭代所有边框，包括行和列
* `range`: 要迭代边框的目标区域
* `iterator`: 用户代码提供的回调函数，用于检查从工作表中检索的所有边框。该函数接受以下参数：
    * `int` Row: 边框所在的行号
    * `int` Column: 边框所在的列号
    * `int` Span: 从此起始位置开始具有相同样式的连续边框数量
    * `RangeBorderStyle` - 包含边框的颜色和样式详情
    * `bool` 返回值: 返回`true`将继续迭代，返回`false`将终止迭代过程

例如，遍历所有水平边框，如果单元格位置有边框，则将单元格背景标记为红色：

```aardio
// 在指定区域内设置水平内部边框
sheet1.Ranges["C3:F10"].BorderInsideHorizontal = ReoGrid.RangeBorderStyle.BlackSolid;
// 在整个工作表区域内迭代水平边框
sheet1.IterateBorders(ReoGrid.RowOrColumn.Row, ReoGrid.RangePosition.EntireRange, function(r, c, span, border) {
    // 为有边框的单元格应用红色背景
    sheet1.Cells[r, c].Style.BackColor = ReoGrid.Graphics.SolidColor.LightCoral;
    return true; // 继续迭代
});
```

![277](../images/277.png)

迭代所有垂直边框：

```aardio
// 设置边框
sheet1.Ranges["C3:F10"].BorderInsideVertical = ReoGrid.RangeBorderStyle.BlackSolid;
// 迭代边框
sheet1.IterateBorders(ReoGrid.RowOrColumn.Column, ReoGrid.RangePosition.EntireRange, function(r, c, span, border) {
    sheet1.Cells[r, c].Style.BackColor = ReoGrid.Graphics.SolidColor.LightCoral;
    return true;
});
```

![278](../images/278.png)

迭代所有边框，包括行和列边框：

```aardio
// 设置边框
sheet1.Ranges["C3:F10"].BorderOutside = ReoGrid.RangeBorderStyle.BlackBoldSolid;
sheet1.Ranges["C3:F10"].BorderInsideHorizontal = ReoGrid.RangeBorderStyle.BlackSolid;
sheet1.Ranges["C3:F10"].BorderInsideVertical = ReoGrid.RangeBorderStyle.BlackDotted;
// 迭代边框
sheet1.IterateBorders(ReoGrid.RowOrColumn.Both, ReoGrid.RangePosition.EntireRange, function(r, c, span, border) {
    sheet1.Cells[r, c].Style.BackColor = ReoGrid.Graphics.SolidColor.LightCoral;
    return true;
});
```

![279](../images/279.png)

## 移除边框

要从工作表中移除边框，可以使用`RemoveRangeBorders`方法：

```aardio
// 从工作表中移除指定区域的所有边框
sheet1.RemoveRangeBorders(ReoGrid.RangePosition(2, 1, 5, 1), ReoGrid.BorderPositions.All);
```

或者，将边框样式设置为`RangeBorderStyle.Empty`也可以有效移除边框：

```aardio
// 将指定区域的边框样式设置为'Empty'以移除边框
sheet1.SetRangeBorders(ReoGrid.RangePosition(2, 1, 5, 3), ReoGrid.BorderPositions.All, ReoGrid.RangeBorderStyle.Empty);
```

### 通过操作移除边框

要通过可撤销的操作移除边框，可以如下使用`DoAction`方法：

```aardio
reoGridControl.DoAction(reoGridControl.CurrentWorksheet,
    ReoGrid.Actions.RemoveRangeBorderAction(
        ReoGrid.RangePosition(2, 1, 5, 1), 
        ReoGrid.ReoGridBorderPos.All
    )
);
```

可以通过调用`Undo`方法撤销此操作：

```aardio
// 撤销边框移除操作
reoGridControl.Undo();
```