Popular way (1, 2) for custom painting of items in DataGridViewComboBox is handling of event DataGridView1.EditingControlShowing and setting up DrawItem event handlers there:
private void dataGridView1_EditingControlShowing(
object sender,
DataGridViewEditingControlShowingEventArgs e)
{
theBoxCell = (ComboBox) e.Control;
theBoxCell.DrawItem += theBoxCell_DrawItem;
theBoxCell.DrawMode = DrawMode.OwnerDrawVariable;
}
You see what is wrong: it uses control-level event to handle work for columns. But what if I have 50+ datagridviews? Painting of custom combo boxes should be handled per column instance, leaving control level intact.
Minimal implementation of per-column handling is below. The only problem I have is that OnDrawItem()
method is not getting called. What am I missing? (You can see that fuchsia background override from the same class is working.)
(To reproduce, just paste the following into class file and add column of type DataGridViewCustomPaintComboBoxColumn to your DataGridView1.)
public class DataGridViewCustomPaintComboBoxColumn : DataGridViewComboBoxColumn
{
public DataGridViewCustomPaintComboBoxColumn()
{
base.New();
CellTemplate = new DataGridViewCustomPaintComboBoxCell();
}
}
public class DataGridViewCustomPaintComboBoxCell : DataGridViewComboBoxCell
{
public override Type EditType {
get { return typeof(DataGridViewCustomPaintComboBoxEditingControl); }
}
protected override void Paint(...)
{
//painting stuff for incative cells here - works well
}
}
public class DataGridViewCustomPaintComboBoxEditingControl : DataGridViewComboBoxEditingControl
{
public override Color BackColor { // property override only for testing
get { return Color.Fuchsia; } // test value works as expected
set { base.BackColor = value; }
}
protected override void OnPaint(PaintEventArgs e) // never called - why?
{
base.OnPaint(e)
}
protected override void OnDrawItem(DrawItemEventArgs e) // never called - why?
{
base.OnDrawItem(e)
}
}