Solved

Datagridview cell with background image

Posted on 2014-03-22
3
1,360 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
Comment Utility
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
Comment Utility
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
Comment Utility
Own issue resolved
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

7 Experts available now in Live!

Get 1:1 Help Now