2

我使用DataGridViewDataGridViewComboBoxColumn,我需要在组合框项左侧添加图标。我目前使用EditingControlShowing事件与ComboBox.DrawItem事件一起,像这样:DatagridViewComboBoxColumn的自定义绘制

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的下拉按钮消失。

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

回答

2

在你CellPainting情况下,可以尝试画在现有的控制:

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

//Draw your stuff 

e.Handled = true; 

或可考虑ComboBoxRenderer类的DrawDropDownButton方法(或ControlPaint.DrawComboButton非视觉样式)。

+0

是的,至于现在我使用这个[MSDN文章]中描述的方式(http://msdn.microsoft.com/en-us/library/hta8z9sz%28v=VS.80%29。 ASPX)。我使用'ComboBoxRenderer',但感谢'ControlPaint',这更适合我。不过,我仍然没有想出如何在与ComboBoxColumn相同的平面样式中绘制该按钮。 'ButtonState.Flat'有点不同。 –