DataGridViewCellPaintingEventArgs 類別

定義

提供 CellPainting 事件的資料。

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

範例

下列程式碼範例說明如何使用此類型。 如需詳細資訊,請參閱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

備註

事件 CellPainting 會針對 上 DataGridView 可見的每個 DataGridViewCell 引發。 若要改善效能,請將 中的 DataGridViewCellPaintingEventArgs 屬性設定為變更資料格的外觀,而不是直接存取 中的 DataGridView 儲存格。 如果您手動繪製儲存格,請將 HandledEventArgs.Handled 屬性設定為 true 。 如果您未設定 HandledEventArgs.Handledtrue ,資料格將會繪製在自訂專案上。

建構函式

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

初始化 DataGridViewCellPaintingEventArgs 類別的新執行個體。

屬性

AdvancedBorderStyle

取得目前 DataGridViewCell 的邊框樣式。

CellBounds

取得目前 DataGridViewCell 的界限。

CellStyle

取得目前 DataGridViewCell 的儲存格樣式。

ClipBounds

取得需要重新繪製的 DataGridView 區域。

ColumnIndex

取得目前 DataGridViewCell 的資料行索引。

ErrorText

取得字串,表示目前 DataGridViewCell 的錯誤訊息。

FormattedValue

取得目前 DataGridViewCell 的已格式化值。

Graphics

取得用來繪製目前 GraphicsDataGridViewCell

Handled

取得或設定值,指出事件處理常式已經完全處理事件或系統應繼續自己的處理。

(繼承來源 HandledEventArgs)
PaintParts

要繪製的儲存格部分。

RowIndex

取得目前 DataGridViewCell 的資料列索引。

State

取得目前 DataGridViewCell 的狀態。

Value

取得目前 DataGridViewCell 的值。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Paint(Rectangle, DataGridViewPaintParts)

繪製所指定界限內區域之指定的儲存格部分。

PaintBackground(Rectangle, Boolean)

繪製所指定界限內區域的儲存格背景。

PaintContent(Rectangle)

繪製所指定界限內區域的儲存格內容。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱