This post helps to load image in row header cell of a
datagridview. As per my investigation, it is found that we cannot directly load an image into a row header cell (this can be false though). But I figured out a way to load an image in row header cell by converting the image into an icon – then show it using the event
dataGridView1_RowPostPaint (and I’m not sure whether this is the best way).
I have added the ’RedCross’ image in an
imageList in my project. If we try to resize the image to a larger extent, the quality of image will be deformed. Try to load images in the image list with the exact size; we prefer to show it in the row header cell.
private void dataGridView1_RowPostPaint
(object sender, DataGridViewRowPostPaintEventArgs e)
Bitmap myBitmap = new Bitmap(imageList1.Images);
Icon myIcon = Icon.FromHandle(myBitmap.GetHicon());
Graphics graphics = e.Graphics;
int iconHeight = 14;
int iconWidth = 14;
int xPosition = e.RowBounds.X + (dataGridView1.RowHeadersWidth / 2);
int yPosition = e.RowBounds.Y +
((dataGridView1.Rows[e.RowIndex].Height - iconHeight) / 2);
Rectangle rectangle = new Rectangle(xPosition, yPosition, iconWidth, iconHeight);