1

我希望有人可以提供帮助。在我的软件中,我有 dynamic Data Grid Views,它们在制作时都共享通用的 grd 名称。每个都放入动态选项卡中。一切正常,但我想将图像添加到Grid Row Header. 这里有非动态网格视图的示例,但不是动态的,所以我改编并使用了以下代码:

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

    }

}

在搜索完成并将数据发送到网格后,我正在代码中的某个位置调用此代码:

grd.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.grd_RowPostPaint);

但是有一个问题,当软件运行最大化时,图像显示正常,当它以任何大小运行时,除了它们显示的唯一时间是在鼠标悬停时(不是这样编码的,它只是那样发生),当我滚动它们时它们消失在网格的底部......任何想法我做错了什么(我不是一个有很多经验的编码器......请善待)。

4

0 回答 0