Solved

Datagridview cell with background image

Posted on 2014-03-22
3
1,393 Views
Last Modified: 2014-04-09
I have created a DataGridView, one of the cells (DataGridViewTextBoxCell) I would like to have a background image. To do this I have used the following on the CellPainting event.

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        var image = Resources.item_qty_white;
        e.PaintBackground(e.ClipBounds, false);

        e.Graphics.DrawImageUnscaled(image, 1410, e.CellBounds.Top);
    }

Open in new window


This works well and the image is on ever row in the position I want. However, the cell it is sitting in has a DataGridViewTextBoxCell with a numeric value. The image floats on top of this value and thus is hidden. I guess the ideal solution would be to make the DataGridViewTextBoxCell be "TopMost" but I couldn't figure out how to do this.

I then decided to try and make the background image partial transparent so the value underneath would be visual, so I changed my CellPainting code to below.
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        var image = Resources.item_qty_white;

        e.PaintBackground(e.ClipBounds, false);
        image.MakeTransparent(Color.White);
        e.Graphics.DrawImageUnscaled(image, 1410, e.CellBounds.Top);
    }

Open in new window

This again worked and I could see the value with the background image surrounding it as I would like. However the next issue arise when I tried to update the value of the cell. Once I did that the previous value was visible and the new value that I was trying to set was overlapping it. I am now stuck.

Any advice/guidance would be very appreciated.
0
Comment
Question by:Diginari
  • 2
3 Comments
 
LVL 1

Expert Comment

by:Uchihaitachi
ID: 39968586
So, the problem happens when you update the value of the cell... that's quite odd, I can't get any idea. Maybe you could change to some other C# gridview control, to know something about their gridview cells may give you some insights.
0
 

Accepted Solution

by:
Diginari earned 0 total points
ID: 39978760
This was fixed by adding

if (!e.Handled)
        {
            e.Handled = true;
            e.PaintContent(e.CellBounds);
        }   

Open in new window

0
 

Author Closing Comment

by:Diginari
ID: 39988231
Own issue resolved
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

679 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