this project:
in project create new form. create 2 buttons , bindingsource.
for button1 write code:
private void button1_click(object sender, eventargs e) { list<int> lst = new list<int>(); lst.add(1); lst.add(3); lst.add(3); this.bindingsource1.datasource = lst; }
for button2 write code:
private void button2_click(object sender, eventargs e) { this.bindingsource1.datasource = null; }
then add positionchanged event bindingsource , write code:
private void bindingsource1_positionchanged(object sender, eventargs e) { messagebox.show("hi"); }
run application , press button1. in button1 set datasource
list , positionchanged
fired normally. thats ok.
then press button2. set datasource
null. shows 2 messages. see positionchanged
event fired 2 times.
it alse occurs if write this.bindingsource1.clear();
too.
why happening?
the "why" examining source code bindingsource class.
it looks clearing or setting datasource null makes multiple calls raise positionchanged event. avoid in code, unhook event before clearing datasource, rewire event again:
bindingsource1.positionchanged -= bindingsource1_positionchanged; bindingsource1.datasource = null; bindingsource1.positionchanged += bindingsource1_positionchanged;
Comments
Post a Comment