Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I managed to add an image on the Header of a dataGridView.
But in runtime:
1. if I move my mouse cursor over the image header and I dont scroll down, the image is visible.
2. if I scroll down and then I move my mouse cursor over the image header, the image is NOT visible.
3. if I scroll back up to the top, and then I move my mouse cursor over the image header, the image is visible again.

How can I make the image visible all the time?
Thank you.

What I have tried:

I tried 2 separate events, and in both, I get the same issue, with image disappearance.

C#
private void dataGridView1_CellPainting_1(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex == dataGridView1.Columns.Count - 1 && e.RowIndex == -1)
    {
        Rectangle cellRectangle = dataGridView1.GetCellDisplayRectangle(6, 0, true);
        e.PaintBackground(e.ClipBounds, true);
        e.Graphics.DrawImage(new Bitmap(@"instock.png"), cellRectangle.Location.X + 5, 1, 18, 18);
        e.Handled = true;
    }
}

private void dataGridView1_Paint(object sender, PaintEventArgs e)
{
    Rectangle cellRectangle = dataGridView1.GetCellDisplayRectangle(6, 0, true);
    e.Graphics.DrawImage(new Bitmap(@"instock.png"), cellRectangle.Location.X + 5, 1, 18, 18);
}
Posted
Updated 1-Nov-21 0:07am
v2

1 solution

The (mian) problem is that you are trying to use the wrong row index to get the image size:
C#
Rectangle cellRectangle = dataGridView1.GetCellDisplayRectangle(6, 0, true);
When you scroll, row zero is no longer displayed, so it doesn't strictly have any dimensions.

If you use the header row index (-1) then it appears to work for me:
C#
private Image header = Image.FromFile(@"D:\Test Data\butHelp16x16.gif");
private void myDataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
    if (e.RowIndex < 0)
        {
        Rectangle cellRectangle = myDataGridView.GetCellDisplayRectangle(0, -1, true);
        e.PaintBackground(e.ClipBounds, true);
        e.Graphics.DrawImage(header, cellRectangle.Location.X + 5, 1, 18, 18);
        e.Handled = true;
        }
    }

However, horizontal scrolling is still a problem, because the header cell dimensions change as it scrolls, and the image gets "chopped up".
To get round that, you need to invalidate the header cell every time you scroll:
C#
private void myDataGridView_Scroll(object sender, ScrollEventArgs e)
    {
    myDataGridView.InvalidateCell(myDataGridView.Columns[0].HeaderCell);
    }


[edit]
BTW: Don't create Bitmap items willy-nilly - they are a scarce resource, and you need to Dispose of them properly when you are finished. Creating them anew each time you get a Cell Paint event is going to crash your app with an "out of memory" exception pretty quickly!
[/edit]
 
Share this answer
 
v2
Comments
_Q12_ 1-Nov-21 6:14am    
Great answer mister OriginalGriff !!! +5 stars
The image now is visible indiferent of the scroll position - Excelent!
ALSO, I guess Im lucky, it doesnt interfere with Horisontal scroll. The image is linked properly to that header even when I make a small window and scroll horisontally, probably because I used that (cellRectangle.Location.X)

Very good observation with the image, and I immediately repair with a (public? when is outside any event or method) declaration.
Bitmap img1 = new Bitmap(@"instock.png"); and after that, referencing img1 inside my paint event.
OriginalGriff 1-Nov-21 6:44am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900