Datagridview show and hide image in header cell or change the border color in the header cell
How I can add a little icon in the header cell without changi开发者_如何学运维ng its formating and its text? I want to toggle show/hide the image in header cell on right click of the mouse.
My problem is not with mouse events but I do not know how to show and hide an image in a header cell. If it's not possible I would like to toggle the border color or something else so the user can know that something happened.
I've tried already code like this : How to display an image in a datagridview column header?.
my code:
if (e.ColumnIndex == 1 && e.RowIndex == -1)
{
e.PaintBackground(e.ClipBounds, false);
Point pt = e.CellBounds.Location; // where you want the bitmap in the cell
int offset = (e.CellBounds.Width - this.imageList1.ImageSize.Width) / 2;
pt.X += offset;
pt.Y += 1;
this.imageList1.Draw(e.Graphics, pt, 0);
e.Handled = true;
}
A minor problem is that this code remove the CellHeaders text, this I could fix with storing the column names in a string array and to display it by the e.ColumnIndex:
Font drawFont = new Font("Microsoft Sans Serif", 10);
SolidBrush drawBrush = new SolidBrush(Color.Black);
e.Graphics.DrawString(storredColNames[e.ColumnIndex], drawFont, drawBrush, e.CellBounds);
Is there another way to preserve the Col Names?
I tried stuff like this but it seems that I can acces the image just when it paints the cells, but I need to be able to acces the image when I right click, for showing and hiding it. Is there a way to do this ?
精彩评论