Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

How to: Customize the Appearance of Cells in the Windows Forms DataGridView Control

You can customize the appearance of any cell by handling the DataGridView control's CellPainting event. You can extract the DataGridView control's Graphics from the Graphics property of the DataGridViewCellPaintingEventArgs. 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. The ClipBounds property of the DataGridViewCellPaintingEventArgs enables you to restrict your painting operations to the cell that is currently being painted.

In the following code example, you will paint all the cells in a ContactName column using the DataGridView control's color scheme. 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.

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;
            }
        }
    }
}

This example requires:

  • A DataGridView control named dataGridView1 with a ContactName column such as the one in the Customers table in the Northwind sample database.

  • References to the System, System.Windows.Forms, and System.Drawing assemblies.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.