【问题标题】:Custom draw of DatagridViewComboBoxColumnDatagridViewComboBoxColumn 的自定义绘制
【发布时间】:2011-09-20 08:56:05
【问题描述】:

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

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 事件就会被触发,并且单元格会被重新绘制(没有图标)。

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

关于在用户完成单元格编辑后如何绘制图标有什么想法吗?

【问题讨论】:

    标签: c# winforms datagridview datagridviewcomboboxcell custom-draw


    【解决方案1】:

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

    e.PaintBackground(e.ClipBounds, true);
    e.PaintContents(e.ClipBounds);
    
    //Draw your stuff
    
    e.Handled = true;
    

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

    【讨论】:

    • 是的,就目前而言,我正在按照msdn article 中描述的方式使用此事件。我使用的是ComboBoxRenderer,但感谢ControlPaint,这更适合我。但是,我仍然没有弄清楚如何以与ComboBoxColumn 相同的平面样式绘制此按钮。 ButtonState.Flat 有点不同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 2018-10-11
    • 2012-10-09
    • 1970-01-01
    相关资源
    最近更新 更多