Nasıl yapılır: Windows Forms DataGridView Denetiminde Hücrelerin Görünüşünü ÖzelleştirmeHow to: Customize the Appearance of Cells in the Windows Forms DataGridView Control

Denetimin olayını işleyerek herhangi bir hücrenin görünümünü özelleştirebilirsiniz DataGridView CellPainting .You can customize the appearance of any cell by handling the DataGridView control's CellPainting event. İçindeki DataGridView özelliğinden denetim ayıklayabilirsiniz Graphics Graphics DataGridViewCellPaintingEventArgs .You can extract the DataGridView control's Graphics from the Graphics property of the DataGridViewCellPaintingEventArgs. Bununla birlikte Graphics , tüm denetimin görünümünü etkileyebilirsiniz DataGridView , ancak genellikle boyanmış olan hücrenin görünümünü etkilemek isteyeceksiniz.With this Graphics, you can affect the appearance of the entire DataGridView control, but you will usually want to affect only the appearance of the cell that is currently being painted. ClipBoundsÖğesinin özelliği, DataGridViewCellPaintingEventArgs boyama işlemlerinizi Şu anda boyanmış olan hücreyle kısıtlamanıza olanak sağlar.The ClipBounds property of the DataGridViewCellPaintingEventArgs enables you to restrict your painting operations to the cell that is currently being painted.

Aşağıdaki kod örneğinde, ContactName denetimin renk düzenini kullanarak bir sütundaki tüm hücreleri boyayabilirsiniz DataGridView .In the following code example, you will paint all the cells in a ContactName column using the DataGridView control's color scheme. Her hücrenin metin içeriği içinde boyanır Crimson ve bir iç içe dikdörtgen denetimin özelliği ile aynı renkte çizilir DataGridView GridColor .Each cell's text content is painted in Crimson, and an inset rectangle is drawn in the same color as the DataGridView control's GridColor property.

ÖrnekExample

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

Kod DerleniyorCompiling the Code

Bu örnek şunları gerektirir:This example requires:

  • DataGridView dataGridView1 ContactName Northwind örnek veritabanındaki Customers tablosunda bulunan gibi bir sütunla adlı bir denetim.A DataGridView control named dataGridView1 with a ContactName column such as the one in the Customers table in the Northwind sample database.

  • Sisteme, System. Windows. Forms ve System. Drawing derlemelerine başvurular.References to the System, System.Windows.Forms, and System.Drawing assemblies.

Ayrıca bkz.See also