Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataGridViewCellPaintingEventArgs Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Provides data for the CellPainting event.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)


public class DataGridViewCellPaintingEventArgs : HandledEventArgs


Gets the border style of the current DataGridViewCell.


Get the bounds of the current DataGridViewCell.


Gets the cell style of the current DataGridViewCell.


Gets the area of the DataGridView that needs to be repainted.


Gets the column index of the current DataGridViewCell.


Gets a string that represents an error message for the current DataGridViewCell.


Gets the formatted value of the current DataGridViewCell.


Gets the Graphics used to paint the current DataGridViewCell.


Gets or sets a value that indicates whether the event handler has completely handled the event or whether the system should continue its own processing.(Inherited from HandledEventArgs.)


The cell parts that are to be painted.


Gets the row index of the current DataGridViewCell.


Gets the state of the current DataGridViewCell.


Gets the value of the current DataGridViewCell.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodPaint(Rectangle, DataGridViewPaintParts)

Paints the specified parts of the cell for the area in the specified bounds.

System_CAPS_pubmethodPaintBackground(Rectangle, Boolean)

Paints the cell background for the area in the specified bounds.


Paints the cell content for the area in the specified bounds.


Returns a string that represents the current object.(Inherited from Object.)

The CellPainting event is raised for each DataGridViewCell that is visible on a DataGridView. To improve performance, set the properties in a DataGridViewCellPaintingEventArgs to change the appearance of the cell instead of directly accessing a cell in the DataGridView. If you manually paint the cell, set the HandledEventArgs.Handled property to true. If you do not set HandledEventArgs.Handled to true, the cell will paint over your customizations.

The following code example illustrates the use of this type. For more information, see How to: Customize the Appearance of Cells in the Windows Forms DataGridView Control.

private void dataGridView1_CellPainting(object sender,
System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
    if (this.dataGridView1.Columns["ContactName"].Index ==
        e.ColumnIndex && e.RowIndex >= 0)
        Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
            e.CellBounds.Y + 1, e.CellBounds.Width - 4,
            e.CellBounds.Height - 4);

        using (
            Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
            backColorBrush = new SolidBrush(e.CellStyle.BackColor))
            using (Pen gridLinePen = new Pen(gridBrush))
                // Erase the cell.
                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                // Draw the grid lines (only the right and bottom lines;
                // DataGridView takes care of the others).
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                    e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                    e.CellBounds.Bottom - 1);
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                    e.CellBounds.Top, e.CellBounds.Right - 1,

                // Draw the inset highlight box.
                e.Graphics.DrawRectangle(Pens.Blue, newRect);

                // Draw the text content of the cell, ignoring alignment.
                if (e.Value != null)
                    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                        Brushes.Crimson, e.CellBounds.X + 2,
                        e.CellBounds.Y + 2, StringFormat.GenericDefault);
                e.Handled = true;

.NET Framework
Available since 2.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
© 2018 Microsoft