.net - How to make custom DataGridViewComboBox dependent only on its DataGridViewComboBoxColumn? -
popular way (1, 2) custom painting of items in datagridviewcombobox handling of event datagridview1.editingcontrolshowing , setting 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 wrong: uses control-level event handle work columns. if have 50+ datagridviews? painting of custom combo boxes should handled per column instance, leaving control level intact.
minimal implementation of per-column handling below. problem have ondrawitem()
method not getting called. missing? (you can see fuchsia background override the same class working.)
(to reproduce, paste following class file , add column of type datagridviewcustompaintcomboboxcolumn datagridview1.)
public class datagridviewcustompaintcomboboxcolumn : datagridviewcomboboxcolumn { public datagridviewcustompaintcomboboxcolumn() { base.new(); celltemplate = new datagridviewcustompaintcomboboxcell(); } } public class datagridviewcustompaintcomboboxcell : datagridviewcomboboxcell { public override type edittype { { return typeof(datagridviewcustompaintcomboboxeditingcontrol); } } protected override void paint(...) { //painting stuff incative cells here - works } } public class datagridviewcustompaintcomboboxeditingcontrol : datagridviewcomboboxeditingcontrol { public override color backcolor { // property override testing { return color.fuchsia; } // test value works 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) } }
the problem have ondrawitem() method not getting called. missing?
what see inside focussed datagridviewcomboboxcell
pretty 'real' combobox
, need reference in order hook drawitem
event! (you can have controls collection of dgv see there 1 more when comboboxcell has focus.. datagridviewcomboboxeditingcontrol
directly descended combobox
)
note: 1 difference out of box combbox
is has set drawmode = ownerdrawvariable
default. if want replace create yourself, don't forget set in code!
looks missing!
if can create own datagridviewcomboboxeditingcontrol
, make appear in dgv you're set.
an alternative model use sort of 'decorator', implements custom drawing columns register it.
dgvcombboxpainter.register(datagridview dgv, stringorint columnnameorindex..)
in registration function hook necessary dgv methods, editingcontrolshowing
@ combobox
see, when dgv has focus , cellpainting
other cases.
the decorator can static , needs 1 combobox
columns have registered, 1 can have focus @ time..
if can handle painting code in decorator better. events can reach datagridview
via sender
, cells' properties via datagridviewcellpaintingeventargs
parameter..
you can put reference current cell combobox
reference's tag
:
theboxcell.tag = datagridview1.currentcell;
if want provide individual delegates call in paint event(s) when registering. have keep list of those, maybe in dictionary..
Comments
Post a Comment