Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

c# - Custom draw of DatagridViewComboBoxColumn

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...