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'有點不同。 –