如何在DataGridView中使用DataGridViewComboBoxColumn显示选定的图像?

4

我在使用DataGridViewComboBoxColumn时遇到了问题,希望能让用户从一组图片中选择一张图片。我看了题为“Custom draw of DatagridViewComboBoxColumn”的讨论 链接 后,也面临着同样的问题:只有当单元格处于编辑模式时,图片才会被绘制出来;当我点击组合框单元格之外的任何位置时,所选的图片会消失!我已经实现了CellPainting事件来重新绘制图片,但仍然无法解决问题。我用以下代码测试了DataGridViewComboBoxColumn:

    public Form1()
    {
        InitializeComponent();
        .....
        imageList.Images.Add(Properties.Resources.icon_priority_low);
        imageList.Images.Add(Properties.Resources.icon_priority_medium);
        .....
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        .....            
        DataGridViewComboBoxCell dgvcbc = (DataGridViewComboBoxCell)newDataGridView1.Rows[0].Cells[1];
        dgvcbc.Items.Add("test0");
        dgvcbc.Items.Add("test1");
        .....        
    }

     private void newDataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control is ComboBox)
        {
            ComboBox theCB = (ComboBox)e.Control;
            theCB.DrawMode = DrawMode.OwnerDrawFixed;
            try
            {
                theCB.DrawItem -= combobox1_DrawItem;
            }
            catch { }
            theCB.DrawItem += combobox1_DrawItem;
        }
    }

    private void combobox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        Graphics g = e.Graphics;
        Brush br = SystemBrushes.WindowText;
        Brush brBack;
        Rectangle rDraw;
        bool bSelected = e.State == DrawItemState.Selected;
        bool bValue = e.State == DrawItemState.ComboBoxEdit;

        if ((e.Index < 0) || (columnIndex != 1))
            return;

        rDraw = e.Bounds;
        rDraw.Inflate(-1, -1);

        int x, y;

        x = e.Bounds.Left + 25;
        y = e.Bounds.Top + 1;
        int midX = (int)(e.Bounds.Width / 2) + e.Bounds.Left;

        // Show image and ignore text.
        g.DrawImage(imageList.Images[e.Index], new Rectangle(midX - 6, y + 2, 12, 12));                     
    }

    private void newDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (columnIndex != 1)
            return;

        Graphics g = e.Graphics;
        Rectangle rDraw = newDataGridView1.GetCellDisplayRectangle(columnIndex, rowIndex, true);

        e.PaintBackground(e.ClipBounds, true);
        e.PaintContent(e.ClipBounds);

        using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
        {
            int y = rDraw.Y + 1;
            int midX = (int)(rDraw.Width / 2) + rDraw.X;

            g.DrawImage(imageList.Images[0], new Rectangle(midX - 6, y + 2, 12, 12));

            e.PaintContent(e.ClipBounds);
            e.Handled = true;
        }
    }
}

如果我点击DataGridView的其他单元格,该单元格将显示“test0”而不是Images [0]。您能帮忙解决这个问题吗?非常感谢。
1个回答

0

PaintContent() 的最后一次调用会擦除您绘制的图像。

在绘制图像之前,您必须先绘制单元格(但不是前景)。它看起来应该像这样:

private void newDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (columnIndex != 1)
        return;

    Graphics g = e.Graphics;
    Rectangle rDraw = newDataGridView1.GetCellDisplayRectangle(columnIndex, rowIndex, true);

    e.Paint(e.CellBounds, e.PaintParts & ~DataGridViewPaintParts.ContentForeground);

    int y = rDraw.Y + 1;
    int midX = (int)(rDraw.Width / 2) + rDraw.X;

    g.DrawImage(imageList.Images[0], new Rectangle(midX - 6, y + 2, 12, 12));

    e.Handled = true;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接