vb.net - Subclassing DatagridView, onPaint event -
i subclassing datagridview extend , bind common functionailty.
here overrides few (about 10) events , work ok except onpaint event.
code:
imports system.componentmodel public class xdatagridview inherits datagridview private _selected_row integer protected overrides sub onpaint(byval e system.windows.forms.painteventargs) sel_row = me.currentrow.index + 1 mybase.onpaint(e) end sub <browsable(true)> _ public property sel_row() integer return _selected_row end set(byval value integer) _selected_row = value end set end property end class
in main form contains class under _paint event handler selected row common property: mysel_row = mydgv.sel_row
when try open designer main form in vbide on place of dgv big red 'x' red border.
system.nullreferenceexception: object reference not set instance of object.
but if start program works normally. other events class work , doesn't report that.
what may cause of error?
i think problem current row datagridview in design view null. that's what's causing system.nullreferenceexception, , if read message box comes , tells error, says error on line: sel_row = me.currentrow.index + 1
. 1 way fix problem this:
protected overrides sub onpaint(byval e system.windows.forms.painteventargs) if (not isnothing(me.currentrow)) sel_row = me.currentrow.index + 1 mybase.onpaint(e) end sub
i pasted code posted , got error did, , adding conditional statement check if current row null keeps exception being thrown when you're in design view. (you'll have build project rid of error message that's in control.) hth
Comments
Post a Comment