DataGridViewAdvancedBorderStyle 类

定义

包含 DataGridView 控件中的单元格的边框样式。

public ref class DataGridViewAdvancedBorderStyle sealed : ICloneable
public sealed class DataGridViewAdvancedBorderStyle : ICloneable
type DataGridViewAdvancedBorderStyle = class
    interface ICloneable
Public NotInheritable Class DataGridViewAdvancedBorderStyle
Implements ICloneable
继承
DataGridViewAdvancedBorderStyle
实现

示例

下面的代码示例调整 的单元格边框 DataGridView ,以便用双边框将内部单元格和左上角的标题单元格与行标题和列标题分隔开来。 此示例演示如何重写 AdjustColumnHeaderBorderStyleAdjustRowHeaderBorderStyleAdjustCellBorderStyle 方法和 AdjustedTopLeftHeaderBorderStyle 属性。 这些成员使用 DataGridViewAdvancedBorderStyle 对象来修改单个单元格边框。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace DataGridViewAdvancedBorderStyleSample
{
    class Form1 : Form
    {
        [STAThreadAttribute()]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }

        public Form1()
        {
            this.AutoSize = true;
            this.Controls.Add(new CustomDataGridView());
            this.Text = "DataGridView advanced border styles demo";
        }
    }

    public class CustomDataGridView : DataGridView
    {
        public CustomDataGridView()
        {
            this.RowTemplate = new DataGridViewCustomRow();
            this.Columns.Add(new DataGridViewCustomColumn());
            this.Columns.Add(new DataGridViewCustomColumn());
            this.Columns.Add(new DataGridViewCustomColumn());
            this.RowCount = 4;
            this.EnableHeadersVisualStyles = false;
            this.AutoSize = true;
        }

        public override DataGridViewAdvancedBorderStyle AdjustedTopLeftHeaderBorderStyle
        {
            get
            {
                DataGridViewAdvancedBorderStyle newStyle =
                    new DataGridViewAdvancedBorderStyle();
                newStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
                newStyle.Left = DataGridViewAdvancedCellBorderStyle.None;
                newStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Outset;
                newStyle.Right = DataGridViewAdvancedCellBorderStyle.OutsetDouble;
                return newStyle;
            }
        }

        public override DataGridViewAdvancedBorderStyle AdjustColumnHeaderBorderStyle(
            DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStyleInput,
            DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStylePlaceHolder,
            bool firstDisplayedColumn,
            bool lastVisibleColumn)
        {
            // Customize the left border of the first column header and the
            // bottom border of all the column headers. Use the input style for 
            // all other borders.
            dataGridViewAdvancedBorderStylePlaceHolder.Left = firstDisplayedColumn ?
                DataGridViewAdvancedCellBorderStyle.OutsetDouble :
                DataGridViewAdvancedCellBorderStyle.None;
            dataGridViewAdvancedBorderStylePlaceHolder.Bottom =
                DataGridViewAdvancedCellBorderStyle.Single;

            dataGridViewAdvancedBorderStylePlaceHolder.Right =
                dataGridViewAdvancedBorderStyleInput.Right;
            dataGridViewAdvancedBorderStylePlaceHolder.Top =
                dataGridViewAdvancedBorderStyleInput.Top;

            return dataGridViewAdvancedBorderStylePlaceHolder;
        }
    }

    public class DataGridViewCustomColumn : DataGridViewColumn
    {
        public DataGridViewCustomColumn()
        {
            this.CellTemplate = new DataGridViewCustomCell();
        }
    }

    public class DataGridViewCustomCell : DataGridViewTextBoxCell
    {
        public override DataGridViewAdvancedBorderStyle AdjustCellBorderStyle(
            DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStyleInput,
            DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStylePlaceHolder,
            bool singleVerticalBorderAdded,
            bool singleHorizontalBorderAdded,
            bool firstVisibleColumn,
            bool firstVisibleRow)
        {
            // Customize the top border of cells in the first row and the 
            // right border of cells in the first column. Use the input style 
            // for all other borders.
            dataGridViewAdvancedBorderStylePlaceHolder.Left = firstVisibleColumn ?
                DataGridViewAdvancedCellBorderStyle.OutsetDouble :
                DataGridViewAdvancedCellBorderStyle.None;
            dataGridViewAdvancedBorderStylePlaceHolder.Top = firstVisibleRow ?
                DataGridViewAdvancedCellBorderStyle.InsetDouble :
                DataGridViewAdvancedCellBorderStyle.None;

            dataGridViewAdvancedBorderStylePlaceHolder.Right =
                dataGridViewAdvancedBorderStyleInput.Right;
            dataGridViewAdvancedBorderStylePlaceHolder.Bottom =
                dataGridViewAdvancedBorderStyleInput.Bottom;

            return dataGridViewAdvancedBorderStylePlaceHolder;
        }
    }

    public class DataGridViewCustomRow : DataGridViewRow
    {
        public override DataGridViewAdvancedBorderStyle AdjustRowHeaderBorderStyle(
            DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStyleInput,
            DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStylePlaceHolder,
            bool singleVerticalBorderAdded,
            bool singleHorizontalBorderAdded,
            bool isFirstDisplayedRow,
            bool isLastDisplayedRow)
        {
            // Customize the top border of the first row header and the
            // right border of all the row headers. Use the input style for 
            // all other borders.
            dataGridViewAdvancedBorderStylePlaceHolder.Top = isFirstDisplayedRow ?
                DataGridViewAdvancedCellBorderStyle.InsetDouble :
                DataGridViewAdvancedCellBorderStyle.None;
            dataGridViewAdvancedBorderStylePlaceHolder.Right =
                DataGridViewAdvancedCellBorderStyle.OutsetDouble;

            dataGridViewAdvancedBorderStylePlaceHolder.Left =
                dataGridViewAdvancedBorderStyleInput.Left;
            dataGridViewAdvancedBorderStylePlaceHolder.Bottom =
                dataGridViewAdvancedBorderStyleInput.Bottom;

            return dataGridViewAdvancedBorderStylePlaceHolder;
        }
    }
}
Imports System.Drawing
Imports System.Windows.Forms


Namespace DataGridViewAdvancedBorderStyleSample

    Class Form1
        Inherits Form

        <STAThreadAttribute()> _
        Public Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub

        Public Sub New()
            Me.AutoSize = True
            Me.Controls.Add(New CustomDataGridView())
            Me.Text = "DataGridView advanced border styles demo"
        End Sub
    End Class

    Public Class CustomDataGridView
        Inherits DataGridView

        Public Sub New()
            With Me
                .RowTemplate = New DataGridViewCustomRow()
                .Columns.Add(New DataGridViewCustomColumn())
                .Columns.Add(New DataGridViewCustomColumn())
                .Columns.Add(New DataGridViewCustomColumn())
                .RowCount = 4
                .EnableHeadersVisualStyles = False
                .AutoSize = True
            End With
        End Sub

        Public Overrides ReadOnly Property AdjustedTopLeftHeaderBorderStyle() _
            As DataGridViewAdvancedBorderStyle
            Get
                Dim newStyle As New DataGridViewAdvancedBorderStyle()
                With newStyle
                    .Top = DataGridViewAdvancedCellBorderStyle.None
                    .Left = DataGridViewAdvancedCellBorderStyle.None
                    .Bottom = DataGridViewAdvancedCellBorderStyle.Outset
                    .Right = DataGridViewAdvancedCellBorderStyle.OutsetDouble
                End With
                Return newStyle
            End Get
        End Property

        Public Overrides Function AdjustColumnHeaderBorderStyle( _
            ByVal dataGridViewAdvancedBorderStyleInput As DataGridViewAdvancedBorderStyle, _
            ByVal dataGridViewAdvancedBorderStylePlaceHolder As DataGridViewAdvancedBorderStyle, _
            ByVal firstDisplayedColumn As Boolean, ByVal lastVisibleColumn As Boolean) _
            As DataGridViewAdvancedBorderStyle

            ' Customize the left border of the first column header and the
            ' bottom border of all the column headers. Use the input style for 
            ' all other borders.
            If firstDisplayedColumn Then
                dataGridViewAdvancedBorderStylePlaceHolder.Left = _
                    DataGridViewAdvancedCellBorderStyle.OutsetDouble
            Else
                dataGridViewAdvancedBorderStylePlaceHolder.Left = _
                    DataGridViewAdvancedCellBorderStyle.None
            End If

            With dataGridViewAdvancedBorderStylePlaceHolder
                .Bottom = DataGridViewAdvancedCellBorderStyle.Single
                .Right = dataGridViewAdvancedBorderStyleInput.Right
                .Top = dataGridViewAdvancedBorderStyleInput.Top
            End With

            Return dataGridViewAdvancedBorderStylePlaceHolder
        End Function
    End Class

    Public Class DataGridViewCustomColumn
        Inherits DataGridViewColumn

        Public Sub New()
            Me.CellTemplate = New DataGridViewCustomCell()
        End Sub
    End Class

    Public Class DataGridViewCustomCell
        Inherits DataGridViewTextBoxCell

        Public Overrides Function AdjustCellBorderStyle( _
            ByVal dataGridViewAdvancedBorderStyleInput As DataGridViewAdvancedBorderStyle, _
            ByVal dataGridViewAdvancedBorderStylePlaceHolder As DataGridViewAdvancedBorderStyle, _
            ByVal singleVerticalBorderAdded As Boolean, _
            ByVal singleHorizontalBorderAdded As Boolean, _
            ByVal firstVisibleColumn As Boolean, _
            ByVal firstVisibleRow As Boolean) As DataGridViewAdvancedBorderStyle

            ' Customize the top border of cells in the first row and the 
            ' right border of cells in the first column. Use the input style 
            ' for all other borders.
            If firstVisibleColumn Then
                dataGridViewAdvancedBorderStylePlaceHolder.Left = _
                    DataGridViewAdvancedCellBorderStyle.OutsetDouble
            Else
                dataGridViewAdvancedBorderStylePlaceHolder.Left = _
                    DataGridViewAdvancedCellBorderStyle.None
            End If

            If firstVisibleRow Then
                dataGridViewAdvancedBorderStylePlaceHolder.Top = _
                    DataGridViewAdvancedCellBorderStyle.InsetDouble
            Else
                dataGridViewAdvancedBorderStylePlaceHolder.Top = _
                    DataGridViewAdvancedCellBorderStyle.None
            End If

            With dataGridViewAdvancedBorderStylePlaceHolder
                .Right = dataGridViewAdvancedBorderStyleInput.Right
                .Bottom = dataGridViewAdvancedBorderStyleInput.Bottom
            End With

            Return dataGridViewAdvancedBorderStylePlaceHolder
        End Function
    End Class

    Public Class DataGridViewCustomRow
        Inherits DataGridViewRow

        Public Overrides Function AdjustRowHeaderBorderStyle( _
            ByVal dataGridViewAdvancedBorderStyleInput As DataGridViewAdvancedBorderStyle, _
            ByVal dataGridViewAdvancedBorderStylePlaceHolder As DataGridViewAdvancedBorderStyle, _
            ByVal singleVerticalBorderAdded As Boolean, _
            ByVal singleHorizontalBorderAdded As Boolean, _
            ByVal isFirstDisplayedRow As Boolean, _
            ByVal isLastDisplayedRow As Boolean) As DataGridViewAdvancedBorderStyle

            ' Customize the top border of the first row header and the
            ' right border of all the row headers. Use the input style for 
            ' all other borders.
            If isFirstDisplayedRow Then
                dataGridViewAdvancedBorderStylePlaceHolder.Top = _
                    DataGridViewAdvancedCellBorderStyle.InsetDouble
            Else
                dataGridViewAdvancedBorderStylePlaceHolder.Top = _
                    DataGridViewAdvancedCellBorderStyle.None
            End If

            With dataGridViewAdvancedBorderStylePlaceHolder
                .Right = DataGridViewAdvancedCellBorderStyle.OutsetDouble
                .Left = dataGridViewAdvancedBorderStyleInput.Left
                .Bottom = dataGridViewAdvancedBorderStyleInput.Bottom
            End With

            Return dataGridViewAdvancedBorderStylePlaceHolder
        End Function
    End Class

End Namespace

注解

控件 DataGridView 允许你完全自定义其外观,包括单元格和标题的边框。 具有 DataGridViewCellBorderStyleColumnHeadersBorderStyleRowHeadersBorderStyle 属性,可用于设置单元格边框的外观。 但是,如果需要进一步自定义边框,类 DataGridViewAdvancedBorderStyle 允许您在单元格的各个边设置边框的样式。 的 LeftRightTopBottom 属性 DataGridViewAdvancedBorderStyle 分别表示单元格的左、右、上和下边框。 可以在 的 AdvancedCellBorderStyleAdvancedColumnHeadersBorderStyleAdvancedRowHeadersBorderStyle 属性上设置这些属性DataGridView,以便为单元格之间的边框生成各种外观。

构造函数

DataGridViewAdvancedBorderStyle()

初始化 DataGridViewAdvancedBorderStyle 类的新实例。

属性

All

获取或设置单元格所有边框的边框样式。

Bottom

获取或设置单元格下边框的样式。

Left

获取单元格左边框的样式。

Right

获取单元格右边框的样式。

Top

获取单元格上边框的样式。

方法

Equals(Object)

确定指定的对象是否等于当前的 DataGridViewAdvancedBorderStyle

GetHashCode()

用作特定类型的哈希函数。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示 DataGridViewAdvancedBorderStyle 的字符串。

显式接口实现

ICloneable.Clone()

创建作为当前实例副本的新对象。

适用于

另请参阅