DataGridViewCellPaintingEventArgs DataGridViewCellPaintingEventArgs DataGridViewCellPaintingEventArgs DataGridViewCellPaintingEventArgs Class

定义

CellPainting 事件提供数据。Provides data for the CellPainting event.

public ref class DataGridViewCellPaintingEventArgs : System::ComponentModel::HandledEventArgs
public class DataGridViewCellPaintingEventArgs : System.ComponentModel.HandledEventArgs
type DataGridViewCellPaintingEventArgs = class
    inherit HandledEventArgs
Public Class DataGridViewCellPaintingEventArgs
Inherits HandledEventArgs
继承
DataGridViewCellPaintingEventArgsDataGridViewCellPaintingEventArgsDataGridViewCellPaintingEventArgsDataGridViewCellPaintingEventArgs

示例

下面的代码示例演示如何使用此类型。The following code example illustrates the use of this type. 有关详细信息,请参阅如何:自定义 Windows 窗体 DataGridView 控件中单元格的外观。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;
            }
        }
    }
}
Private Sub dataGridView1_CellPainting(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) _
    Handles dataGridView1.CellPainting

    If Me.dataGridView1.Columns("ContactName").Index = _
        e.ColumnIndex AndAlso e.RowIndex >= 0 Then

        Dim newRect As New Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, _
            e.CellBounds.Width - 4, e.CellBounds.Height - 4)
        Dim backColorBrush As New SolidBrush(e.CellStyle.BackColor)
        Dim gridBrush As New SolidBrush(Me.dataGridView1.GridColor)
        Dim gridLinePen As New Pen(gridBrush)

        Try

            ' 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 IsNot Nothing) Then
                e.Graphics.DrawString(CStr(e.Value), e.CellStyle.Font, _
                Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, _
                StringFormat.GenericDefault)
            End If
            e.Handled = True

        Finally
            gridLinePen.Dispose()
            gridBrush.Dispose()
            backColorBrush.Dispose()
        End Try

    End If

End Sub

注解

对于中显示的每个DataGridViewCell DataGridView, 都会引发事件。CellPaintingThe CellPainting event is raised for each DataGridViewCell that is visible on a DataGridView. 若要提高性能, 请将中DataGridViewCellPaintingEventArgs的属性设置为更改单元格的外观, 而不是直接访问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. 如果手动绘制单元格, 请将HandledEventArgs.Handled属性设置为。 trueIf you manually paint the cell, set the HandledEventArgs.Handled property to true. 如果未将设置HandledEventArgs.Handledtrue, 则单元格将在你的自定义项上进行绘制。If you do not set HandledEventArgs.Handled to true, the cell will paint over your customizations.

构造函数

DataGridViewCellPaintingEventArgs(DataGridView, Graphics, Rectangle, Rectangle, Int32, Int32, DataGridViewElementStates, Object, Object, String, DataGridViewCellStyle, DataGridViewAdvancedBorderStyle, DataGridViewPaintParts) DataGridViewCellPaintingEventArgs(DataGridView, Graphics, Rectangle, Rectangle, Int32, Int32, DataGridViewElementStates, Object, Object, String, DataGridViewCellStyle, DataGridViewAdvancedBorderStyle, DataGridViewPaintParts) DataGridViewCellPaintingEventArgs(DataGridView, Graphics, Rectangle, Rectangle, Int32, Int32, DataGridViewElementStates, Object, Object, String, DataGridViewCellStyle, DataGridViewAdvancedBorderStyle, DataGridViewPaintParts) DataGridViewCellPaintingEventArgs(DataGridView, Graphics, Rectangle, Rectangle, Int32, Int32, DataGridViewElementStates, Object, Object, String, DataGridViewCellStyle, DataGridViewAdvancedBorderStyle, DataGridViewPaintParts)

初始化 DataGridViewCellPaintingEventArgs 类的新实例。Initializes a new instance of the DataGridViewCellPaintingEventArgs class.

属性

AdvancedBorderStyle AdvancedBorderStyle AdvancedBorderStyle AdvancedBorderStyle

获取当前 DataGridViewCell 的边框样式。Gets the border style of the current DataGridViewCell.

CellBounds CellBounds CellBounds CellBounds

获取当前 DataGridViewCell 的边界。Get the bounds of the current DataGridViewCell.

CellStyle CellStyle CellStyle CellStyle

获取当前 DataGridViewCell 的单元格样式。Gets the cell style of the current DataGridViewCell.

ClipBounds ClipBounds ClipBounds ClipBounds

获取 DataGridView 中需要重新绘制的区域。Gets the area of the DataGridView that needs to be repainted.

ColumnIndex ColumnIndex ColumnIndex ColumnIndex

获取当前 DataGridViewCell 的列索引。Gets the column index of the current DataGridViewCell.

ErrorText ErrorText ErrorText ErrorText

获取表示当前 DataGridViewCell 的错误消息的字符串。Gets a string that represents an error message for the current DataGridViewCell.

FormattedValue FormattedValue FormattedValue FormattedValue

获取当前 DataGridViewCell 的格式化值。Gets the formatted value of the current DataGridViewCell.

Graphics Graphics Graphics Graphics

获取用于绘制当前 GraphicsDataGridViewCellGets the Graphics used to paint the current DataGridViewCell.

Handled Handled Handled Handled

获取或设置一个值,该值指示事件处理程序是否已完整处理事件,或者系统是否应该继续本身的处理。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)
PaintParts PaintParts PaintParts PaintParts

要绘制的单元格部分。The cell parts that are to be painted.

RowIndex RowIndex RowIndex RowIndex

获取当前 DataGridViewCell 的行索引。Gets the row index of the current DataGridViewCell.

State State State State

获取当前 DataGridViewCell 的状态。Gets the state of the current DataGridViewCell.

Value Value Value Value

获取当前 DataGridViewCell 的值。Gets the value of the current DataGridViewCell.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Paint(Rectangle, DataGridViewPaintParts) Paint(Rectangle, DataGridViewPaintParts) Paint(Rectangle, DataGridViewPaintParts) Paint(Rectangle, DataGridViewPaintParts)

绘制指定边界中相应区域的单元格的指定部分。Paints the specified parts of the cell for the area in the specified bounds.

PaintBackground(Rectangle, Boolean) PaintBackground(Rectangle, Boolean) PaintBackground(Rectangle, Boolean) PaintBackground(Rectangle, Boolean)

绘制指定边界中相应区域的单元格背景。Paints the cell background for the area in the specified bounds.

PaintContent(Rectangle) PaintContent(Rectangle) PaintContent(Rectangle) PaintContent(Rectangle)

绘制指定边界中相应区域的单元格内容。Paints the cell content for the area in the specified bounds.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于

另请参阅