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

Posted on 2007-10-10
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
    LVL 17

    Expert Comment

    by:Shanmuga Sundaram

    Author Comment

    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

    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;

    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    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…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now