DatagridViewComboBoxColumn 的自定义绘制 [英] Custom draw of DatagridViewComboBoxColumn

查看:30
本文介绍了DatagridViewComboBoxColumn 的自定义绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 DataGridViewDataGridViewComboBoxColumn 一起使用,我需要在组合框项的左侧添加图标.我目前正在使用 EditingControlShowing 事件和 ComboBox.DrawItem 事件,如下所示:

I'm using a DataGridView with a DataGridViewComboBoxColumn and I need to add icons to the left of combobox items. I'm currently using EditingControlShowing event along with ComboBox.DrawItem event, like this:

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

private void combobox1_DrawItem(object sender, DrawItemEventArgs e)
{
    // Drawing icon here        
}

问题是只有在单元格处于编辑模式时才会绘制图标.一旦我点击单元格外的某个地方,CellEndEdit 事件就会被触发,并且单元格会被重新绘制(没有图标).

The problem is icons are only drawn as long as the cell is in edit mode. As soon as I click somewhere outside the cell the CellEndEdit event is fired and the cell gets repainted (without the icon).

我尝试使用 DataGridView.CellPainting 事件来解决此问题,但它导致 DataGridViewComboBoxColumn 的下拉按钮消失.

I tried using the DataGridView.CellPainting event to resolve this issue, but it causes the dropdown button of the DataGridViewComboBoxColumn to disappear.

关于如何在用户完成单元格编辑后绘制图标的任何想法?

Any ideas on how to draw an icon after the user finished editing the cell?

推荐答案

在你的 CellPainting 事件中,你可以尝试在现有控件上绘画:

In you CellPainting event, you can try painting over the existing controls:

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

//Draw your stuff

e.Handled = true;

或查看 ComboBoxRenderer 类的 DrawDropDownButton 方法(或 ControlPaint.DrawComboButton 用于非视觉样式).

or look into ComboBoxRenderer class for the DrawDropDownButton method (or ControlPaint.DrawComboButton for non-visual styles).

这篇关于DatagridViewComboBoxColumn 的自定义绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆