Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Datagridview cell with background image

Posted on 2014-03-22
3
Medium Priority
?
1,486 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline
Suggested Courses

972 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