2

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        
}

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).

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?

Dmitrii Erokhin
  • 1,347
  • 13
  • 31

1 Answers1

3

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;

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

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • yes, as for now I'm using this event in way described in this [msdn article](http://msdn.microsoft.com/en-us/library/hta8z9sz%28v=VS.80%29.aspx). I was using `ComboBoxRenderer`, but thanks for `ControlPaint`, that suits me better. However, I still haven't figured out how to draw this button in the same flat style as `ComboBoxColumn` does it. The `ButtonState.Flat` differs a bit. – Dmitrii Erokhin Sep 21 '11 at 05:08