使用DataGridViewComboBoxColumn在DataGridView中显示所选图像?

时间:2012-02-15 06:47:52

标签: c# winforms datagridview datagridviewcomboboxcell custom-draw

我在使用DataGridView和DataGridViewComboBoxColumn让用户从图像列表中选择图像时遇到了问题。在题为"自定义绘制DatagridViewComboBoxColumn"的问题中进行了讨论。 ref Link。我也面临这个问题,因为只有当单元格处于编辑模式时才会绘制图像。当我点击组合框单元外的某个地方时,所选图像将消失!我已经实现了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;
        }
    }
}

单元格将显示&#34; test0&#34;如果我点击DataGridView的其他单元格,而不是Images [0]。你能帮忙解决这个问题吗?非常感谢。

1 个答案:

答案 0 :(得分: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;
}