Custom Cell Painting

Although the DataGridView supports images, there are still cases where you'll want to display different types of content or apply custom formatting that isn't directly supported. For example, you might want to mingle text and graphics, draw shapes, or add a background behind the cell content. All of these details can be handled using the GDI+ drawing tools you learned about in Chapter 7. All you need to do is handle the CellPainting event (or the RowPrePaint and RowPostPaint events to apply drawing effects for the entire row). All of these events provide a drawing surface through the Graphics property of the appropriate EventArgs object.

The following example shows how you can handle the CellPainting event to fill a background gradient behind the cell content of the first column. The only caveats are that you need to explicitly set the DataGridViewCellPaintingEventArgs.Handled property to true to prevent the DataGridView from performing its own painting logic over the top of yours, and you need to paint both the background and the cell content. Here's the drawing logic:

private void DataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

// Only paint the desired column and // don't paint headers.

if ((e.ColumnIndex == 0) && (e.RowIndex >= 0)) {

// If the cell is selected, use the normal painting // instead of the custom painting. if ((e.State & DataGridViewElementStates.Selected) != DataGridViewElementStates.Selected)

// Suppress normal cell painting. e.Handled = true;

// Get the rectangle where painting will take place. Rectangle rect = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width - 1, e.CellBounds.Height - 1);

// Render the custom cell background.

using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.White, Color.YellowGreen, 35f))

e.Graphics.FillRectangle(brush, rect);

// Render the standard cell border.

using (Pen pen = new Pen(dataGridView1.GridColor)) {

e.Graphics.DrawRectangle(pen, e.CellBounds.X - 1, e.CellBounds.Y - 1, e.CellBounds.Width, e.CellBounds.Height);

// Render the cell text.

string cellValue = e.FormattedValue.ToString();

// Set the alignment settings. Unfortunately, there's no // straightforward way to get the cell style settings and // convert them to the text alignment values you need here.

StringFormat format = new StringFormat(); format.LineAlignment = StringAlignment.Center; format.Alignment = StringAlignment.Near;

using (Brush valueBrush = new SolidBrush(e.CellStyle.ForeColor)) {

e.Graphics.DrawString(cellValue, e.CellStyle.Font, valueBrush, rect, format);

Figure 15-16 shows the result.

Datagridview Cellpainting
Figure 15-16. Custom painting in a cell

The CellPainting event provides several shortcuts so you don't need to re-create basic functionality. For example, the DataGridViewCellPaintingEventArgs class provides a PaintContent() and a PaintBackground() method. You can call these to paint part of the cell— for example, if you're interested in adding a fancy background but you don't want to bother drawing the cell text by hand. For even more control, you can use the Paint() method, which accepts a combination of values from the DataGridViewPaintParts enumeration. This combination of values tells the DataGridView exactly what to paint, and it can include the Background, Border, ContentBackground, ContentForeground, ErrorIcon, Focus, and SelectionBackground.

Using the DataGridViewCellPaintingEventArgs.PaintO method, it's possible to simplify the previous example as shown here:

private void DataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

// Paint only the desired column and // don't paint headers.

if ((e.ColumnIndex == 0) && (e.RowIndex >= 0)) {

// If the cell is selected, use the normal painting // instead of the custom painting. if ((e.State & DataGridViewElementStates.Selected) != DataGridViewElementStates.Selected)

e.Handled = true;

// Render the custom cell background.

Rectangle rect = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width - 1, e.CellBounds.Height - 1); using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.White, Color.YellowGreen, 35f))

e.Graphics.FillRectangle(brush, rect);

// Paint the cell text, the border, and the error icon (if needed). // Don't worry about the focus rectangle or selection background, // because we aren't painting selected cells. e.Paint(e.ClipBounds, DataGridViewPaintParts.ContentForeground | DataGridViewPaintParts.Border | DataGridViewPaintParts.ErrorIcon | DataGridViewPaintParts.Focus);

+2 -1

Responses

  • Robert
    How to call CellPainting in c sharp?
    7 years ago
  • Matilda
    How to refer to a function that does cellpainting?
    7 years ago
  • maire
    How to implement the cellpainting in asp.net in web applications?
    7 years ago
  • Hyiab
    How to call datagridview.cellpainting?
    5 years ago
  • tammy
    How to add handle cellpainting in vb.net?
    5 years ago
  • Drogo
    How to paint datagridview cell border?
    4 years ago

Post a comment