DataGridViewCellPaintingEventArgs Class
TOC
Collapse the table of content
Expand the table of content

DataGridViewCellPaintingEventArgs Class

 

Provides data for the CellPainting event.

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

System.Object
  System.EventArgs
    System.ComponentModel.HandledEventArgs
      System.Windows.Forms.DataGridViewCellPaintingEventArgs

public class DataGridViewCellPaintingEventArgs : HandledEventArgs

NameDescription
System_CAPS_pubpropertyAdvancedBorderStyle

Gets the border style of the current DataGridViewCell.

System_CAPS_pubpropertyCellBounds

Get the bounds of the current DataGridViewCell.

System_CAPS_pubpropertyCellStyle

Gets the cell style of the current DataGridViewCell.

System_CAPS_pubpropertyClipBounds

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

System_CAPS_pubpropertyColumnIndex

Gets the column index of the current DataGridViewCell.

System_CAPS_pubpropertyErrorText

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

System_CAPS_pubpropertyFormattedValue

Gets the formatted value of the current DataGridViewCell.

System_CAPS_pubpropertyGraphics

Gets the Graphics used to paint the current DataGridViewCell.

System_CAPS_pubpropertyHandled

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.)

System_CAPS_pubpropertyPaintParts

The cell parts that are to be painted.

System_CAPS_pubpropertyRowIndex

Gets the row index of the current DataGridViewCell.

System_CAPS_pubpropertyState

Gets the state of the current DataGridViewCell.

System_CAPS_pubpropertyValue

Gets the value of the current DataGridViewCell.

NameDescription
System_CAPS_pubmethodEquals(Object)

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

System_CAPS_protmethodFinalize()

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

System_CAPS_pubmethodGetHashCode()

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

System_CAPS_pubmethodGetType()

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

System_CAPS_protmethodMemberwiseClone()

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.

System_CAPS_pubmethodPaintContent(Rectangle)

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

System_CAPS_pubmethodToString()

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,
                    e.CellBounds.Bottom);

                // 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
Show:
© 2016 Microsoft