Custom Draw DatagridViewComboBoxColumn

I am using a DataGridView with a DataGridViewComboBoxColumn , and I need to add icons to the left of the combobox elements. I am currently using the EditingControlShowing event along with the ComboBox.DrawItem event, for example:

 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 that the icons are displayed only as long as the cell is in edit mode. As soon as I click somewhere outside the cell, the CellEndEdit event fires and the cell redraws (without icon).

I tried to use the DataGridView.CellPainting event to solve this problem, but this causes the DataGridViewComboBoxColumn drop-down button to disappear.

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

+2
source share
1 answer

In the CellPainting event, you can try painting on top of existing controls:

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

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

+2
source

Source: https://habr.com/ru/post/1396562/


All Articles