How can I add a watermark behind the cells of a datagridview?

Posted on 2007-10-10
Medium Priority
Last Modified: 2013-12-17
I tried customizing the backgroundimage property, but it doesn't overlay the image onto the cells themselves.  

        protected override void PaintBackground(Graphics graphics, Rectangle clipBounds, Rectangle gridBounds)
            graphics.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(gridBounds, this.BackgroundColor, this.BackgroundColor2, gradientMode), gridBounds);

            if (backgroundImage != null)
            { graphics.DrawImageUnscaled(backgroundImage, gridBounds); }

Question by:winterhowl

Author Comment

ID: 20049536
shasunder: I have seen both of those, but they don't answer my needs.  I need an image to sit behind the cells themselves.  And not just individual cells, but rather across all of them.  One image that covers all visible cells.
LVL 96

Accepted Solution

Bob Learned earned 1000 total points
ID: 20059137
You might be able to extract parts of an image that is sized the same dimensions as the current cells, and then use the CellPainting event handler to paint the parts of the image that correspond to the current cell.  

Here is an example of drawing a red rectangle around the cell borders, but you can modify it to draw image parts for each cell.

private void DataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
    e.PaintBackground(e.ClipBounds, e.State == DataGridViewElementStates.Selected);
    e.Paint(e.ClipBounds, DataGridViewPaintParts.ContentBackground | DataGridViewPaintParts.ContentForeground | DataGridViewPaintParts.Border | DataGridViewPaintParts.ErrorIcon | DataGridViewPaintParts.Focus | DataGridViewPaintParts.SelectionBackground);
    if (e.ColumnIndex > -1 & e.RowIndex > -1) {
        e.Graphics.DrawRectangle(Pens.Red, e.CellBounds);
    e.Handled = true;


Expert Comment

ID: 20237993
Forced accept.

EE Admin

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question