Datagridview cell with background image

UnitStack

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);
    }

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);
    }

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.

Junaith

You have to set e.Handled = true to prevent the system from painting. The below code works as you expected.

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex != -1 && e.ColumnIndex == columnIndex)
    {
        if ((e.PaintParts & DataGridViewPaintParts.Background) != DataGridViewPaintParts.None)
        {
            e.Graphics.DrawImage(Resources.Image1, e.CellBounds);                    
        }
        if (!e.Handled)
        {
            e.Handled = true;
            e.PaintContent(e.CellBounds);
        }            
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

changing the combobox cell background in datagridview

From Dev

Background image for each cell

From Dev

Merge DataGridView Image cell with text cell

From Dev

How to get image value of Datagridview Image Cell

From Dev

Changing the cell background color of a 'DataGridView' in virtual mode

From Dev

Changing the cell background color of a 'DataGridView' in virtual mode

From Dev

Change datagridview cell background based on a external parameter

From Dev

UICollectionViewCell background image out of cell

From Dev

DataGridView Mousedown and Mouseup cell background color change doesn't work

From Dev

UICollectionView Cell with Image, change Background with click

From Dev

Adding a background image to a single table cell

From Dev

UICollectionView Cell with Image, change Background with click in Swift

From Dev

collectionview change cell background image when scrolling

From Dev

CSS clear:both not working for table cell with a background image?

From Dev

How do I set the background image size of a table cell in swift?

From Dev

Swift table view with a cell that is transparent and shows background image

From Dev

CSS table cell background image style (two triangles)

From Dev

How to add background image to a UItable cell which is created dynamically in iOS

From Dev

iTextSharp. Why cell background image is rotated 90 degrees clockwise?

From Dev

how to set background image in uitableview cell in ios7?

From Dev

CSS table cell background image style (two triangles)

From Dev

Turn Off Compressing Cell Background Image - Objective-C (Xcode)

From Dev

Adding bitmap image as table cell background in vb.net

From Dev

How to set a background image to an entry cell in xamarin.forms

From Dev

Change of the edited cell in a datagridview cell

From Dev

DataGridView Numeric Only Cell?

From Dev

Cell Formatting In DataGridView On DataBindingComplete

From Dev

Casting datagridview cell as float

From Dev

Multiline text in DataGridView Cell

Related Related

  1. 1

    changing the combobox cell background in datagridview

  2. 2

    Background image for each cell

  3. 3

    Merge DataGridView Image cell with text cell

  4. 4

    How to get image value of Datagridview Image Cell

  5. 5

    Changing the cell background color of a 'DataGridView' in virtual mode

  6. 6

    Changing the cell background color of a 'DataGridView' in virtual mode

  7. 7

    Change datagridview cell background based on a external parameter

  8. 8

    UICollectionViewCell background image out of cell

  9. 9

    DataGridView Mousedown and Mouseup cell background color change doesn't work

  10. 10

    UICollectionView Cell with Image, change Background with click

  11. 11

    Adding a background image to a single table cell

  12. 12

    UICollectionView Cell with Image, change Background with click in Swift

  13. 13

    collectionview change cell background image when scrolling

  14. 14

    CSS clear:both not working for table cell with a background image?

  15. 15

    How do I set the background image size of a table cell in swift?

  16. 16

    Swift table view with a cell that is transparent and shows background image

  17. 17

    CSS table cell background image style (two triangles)

  18. 18

    How to add background image to a UItable cell which is created dynamically in iOS

  19. 19

    iTextSharp. Why cell background image is rotated 90 degrees clockwise?

  20. 20

    how to set background image in uitableview cell in ios7?

  21. 21

    CSS table cell background image style (two triangles)

  22. 22

    Turn Off Compressing Cell Background Image - Objective-C (Xcode)

  23. 23

    Adding bitmap image as table cell background in vb.net

  24. 24

    How to set a background image to an entry cell in xamarin.forms

  25. 25

    Change of the edited cell in a datagridview cell

  26. 26

    DataGridView Numeric Only Cell?

  27. 27

    Cell Formatting In DataGridView On DataBindingComplete

  28. 28

    Casting datagridview cell as float

  29. 29

    Multiline text in DataGridView Cell

HotTag

Archive