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



使用DataGridView和DataGridViewComboBoxColumn让用户从图像列表中选择图像时遇到问题。在题为"自定义绘制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]。你能帮忙解决这个问题吗。非常感谢。

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

相关内容

  • 没有找到相关文章

最新更新