DataGridViewPaintParts 枚举

定义

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

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

public enum class DataGridViewPaintParts
[System.Flags]
public enum DataGridViewPaintParts
[<System.Flags>]
type DataGridViewPaintParts = 
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 控件中提供的更大示例的一部分。

// 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);
        }
    }
}
' Paints the custom selection background for selected rows.
Sub dataGridView1_RowPrePaint(ByVal sender As Object, _
    ByVal e As DataGridViewRowPrePaintEventArgs) _
    Handles dataGridView1.RowPrePaint

    ' Do not automatically paint the focus rectangle.
    e.PaintParts = e.PaintParts And Not DataGridViewPaintParts.Focus

    ' Determine whether the cell should be painted with the 
    ' custom selection background.
    If (e.State And DataGridViewElementStates.Selected) = _
        DataGridViewElementStates.Selected Then

        ' Calculate the bounds of the row.
        Dim rowBounds As New Rectangle( _
            Me.dataGridView1.RowHeadersWidth, e.RowBounds.Top, _
            Me.dataGridView1.Columns.GetColumnsWidth( _
            DataGridViewElementStates.Visible) - _
            Me.dataGridView1.HorizontalScrollingOffset + 1, _
            e.RowBounds.Height)

        ' Paint the custom selection background.
        Dim backbrush As New _
            System.Drawing.Drawing2D.LinearGradientBrush(rowBounds, _
            Me.dataGridView1.DefaultCellStyle.SelectionBackColor, _
            e.InheritedRowStyle.ForeColor, _
            System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
        Try
            e.Graphics.FillRectangle(backbrush, rowBounds)
        Finally
            backbrush.Dispose()
        End Try
    End If

End Sub

注解

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

适用于

另请参阅