Image in rowheadercell for dynamic gridview
<p>I am hoping someone can help. In my software I have dynamic <code>Data Grid Views</code>, which all share the common grd name when being made. Each placed into dynamic tabs. All works fine, but I wanted to add an image to the <code>Grid Row Header</code>. There are examples on here for non dynamic grid views, but not for dynamic, so I adapted and used the following code:</p>

<pre><code>private void grd_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    if (e.InheritedRowStyle.BackColor == Color.Gold)
    {
        Bitmap myBitmap = new Bitmap(imageList1.Images[0]);
        Icon myIcon = Icon.FromHandle(myBitmap.GetHicon());

        Graphics graphics = e.Graphics;

        //Set Image dimension - Users choice
        int iconHeight = 16;
        int iconWidth = 32;

        //Set x/y position - As the center of the RowHeaderCell
        int xPosition = ((e.RowBounds.Height * 2) - iconWidth) / 2;
        int yPosition = ((e.RowBounds.Y + (e.RowIndex * e.RowBounds.Height) - iconHeight) / 2)+ e.RowBounds.Height + 1;

        Rectangle rectangle = new Rectangle(xPosition, yPosition, iconWidth, iconHeight);
        graphics.DrawIcon(myIcon, rectangle);

    }
    else if (e.InheritedRowStyle.BackColor == Color.PowderBlue)
    {
        Bitmap myBitmap = new Bitmap(imageList1.Images[1]);
        Icon myIcon = Icon.FromHandle(myBitmap.GetHicon());

        Graphics graphics = e.Graphics;

        //Set Image dimension - Users choice
        int iconHeight = 16;
        int iconWidth = 16;

        //Set x/y position - As the center of the RowHeaderCell
        int xPosition = ((e.RowBounds.Height * 2) - iconWidth) / 2;
        int yPosition = ((e.RowBounds.Y + (e.RowIndex * e.RowBounds.Height) - iconHeight) / 2) + e.RowBounds.Height + 1;

        Rectangle rectangle = new Rectangle(xPosition, yPosition, iconWidth, iconHeight);
        graphics.DrawIcon(myIcon, rectangle);
    }
    else if (e.InheritedRowStyle.BackColor == Color.YellowGreen)
    {
        //Convert the image to icon, inorder to load it in the row header column
        Bitmap myBitmap = new Bitmap(imageList1.Images[2]);
        Icon myIcon = Icon.FromHandle(myBitmap.GetHicon());

        Graphics graphics = e.Graphics;

        //Set Image dimension - Users choice
        int iconHeight = 16;
        int iconWidth = 16;

        //Set x/y position - As the center of the RowHeaderCell
        int xPosition = ((e.RowBounds.Height *2) - iconWidth) / 2;
        int yPosition = ((e.RowBounds.Y + (e.RowIndex * e.RowBounds.Height) - iconHeight) / 2) + e.RowBounds.Height + 1;

        Rectangle rectangle = new Rectangle(xPosition, yPosition, iconWidth, iconHeight);
        graphics.DrawIcon(myIcon, rectangle);

    }
    else if (e.InheritedRowStyle.BackColor == Color.White)
    {
        //Convert the image to icon, inorder to load it in the row header column
        Bitmap myBitmap = new Bitmap(imageList1.Images[3]);
        Icon myIcon = Icon.FromHandle(myBitmap.GetHicon());

        Graphics graphics = e.Graphics;

        //Set Image dimension - Users choice
        int iconHeight = 16;
        int iconWidth = 16;

        //Set x/y position - As the center of the RowHeaderCell
        int xPosition = ((e.RowBounds.Height * 2) - iconHeight) / 2;
        int yPosition = ((e.RowBounds.Y + (e.RowIndex * e.RowBounds.Height) - iconHeight) / 2) + e.RowBounds.Height + 1;

        Rectangle rectangle = new Rectangle(xPosition, yPosition, iconWidth, iconHeight);
        graphics.DrawIcon(myIcon, rectangle);

    }

}
</code></pre>

<p>And I am calling this code from a place in the code after the searching is completed and the data sent to the grid using:</p>

<pre><code>grd.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.grd_RowPostPaint);
</code></pre>

<p>There is a problem however when the software runs maximized the images display ok, when it runs at any size other than the only time they display is on mouseover (not coded that way it just happends that way) and when I scroll they disappear at the bottom of the grid ... any ideas what I am doing wrong (I am not a coder with much experience ... please be kind).</p>
