DataGridViewPaintParts 枚举

定义

定义用于指定要绘制的 DataGridViewCell 部分的值。

此枚举支持其成员值的按位组合。

C#
[System.Flags]
public enum DataGridViewPaintParts
继承
DataGridViewPaintParts
属性

字段

名称 说明
All 127

应绘制单元格的所有部分。

Background 1

应绘制单元格的背景。

Border 2

应绘制单元格的边框。

ContentBackground 4

应绘制单元格内容的背景。

ContentForeground 8

应绘制单元格内容的前景。

ErrorIcon 16

应绘制单元格错误图标。

Focus 32

应当在单元格周围绘制聚焦框。

None 0

不应绘制任何内容。

SelectionBackground 64

当选择该单元格时,应绘制该单元格的背景。

示例

下面的代码示例演示了此类型的用法。 此示例是How to: Customize the Appearance of Rows in the Windows 窗体 DataGridView 控件中提供的更大示例的一部分。

C#
// Paints the custom selection background for selected rows.
void dataGridView1_RowPrePaint(object sender,
        DataGridViewRowPrePaintEventArgs e)
{
    // Do not automatically paint the focus rectangle.
    e.PaintParts &= ~DataGridViewPaintParts.Focus;

    // Determine whether the cell should be painted
    // with the custom selection background.
    if ((e.State & DataGridViewElementStates.Selected) ==
                DataGridViewElementStates.Selected)
    {
        // Calculate the bounds of the row.
        Rectangle rowBounds = new Rectangle(
            this.dataGridView1.RowHeadersWidth, e.RowBounds.Top,
            this.dataGridView1.Columns.GetColumnsWidth(
                DataGridViewElementStates.Visible) -
            this.dataGridView1.HorizontalScrollingOffset + 1,
            e.RowBounds.Height);

        // Paint the custom selection background.
        using (Brush backbrush =
            new System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
                this.dataGridView1.DefaultCellStyle.SelectionBackColor,
                e.InheritedRowStyle.ForeColor,
                System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
        {
            e.Graphics.FillRectangle(backbrush, rowBounds);
        }
    }
}

注解

此枚举由受保护的 DataGridViewCell.Paint 方法和控件的 CellPaintingRowPrePaintRowPostPaint 事件的 DataGridView 处理程序使用。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

另请参阅