Edit

Share via


DataGridViewPaintParts Enum

Definition

Defines values for specifying the parts of a DataGridViewCell that are to be painted.

This enumeration supports a bitwise combination of its member values.

C#
[System.Flags]
public enum DataGridViewPaintParts
Inheritance
DataGridViewPaintParts
Attributes

Fields

Name Value Description
None 0

Nothing should be painted.

Background 1

The background of the cell should be painted.

Border 2

The border of the cell should be painted.

ContentBackground 4

The background of the cell content should be painted.

ContentForeground 8

The foreground of the cell content should be painted.

ErrorIcon 16

The cell error icon should be painted.

Focus 32

The focus rectangle should be painted around the cell.

SelectionBackground 64

The background of the cell should be painted when the cell is selected.

All 127

All parts of the cell should be painted.

Examples

The following code example illustrates the use of this type. This example is part of a larger example available in How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control.

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);
        }
    }
}

Remarks

This enumeration is used by the protected DataGridViewCell.Paint method and by handlers for the CellPainting, RowPrePaint, and RowPostPaint events of the DataGridView control.

Applies to

Product Versions
.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

See also