c# - Circular loop when using selection changed datagrdivew -


i using datagradview , wanting prompt user save current row before moving off have been trying use following event seem in circular loop when hit save event.

private void dgstock_selectionchanged(object sender, eventargs e) {         if (isdirty == true)         {             issavedfromrow = true;             btnsavedetails_click(sender, e);              isdirty = false;         } } 

the problem selection changed event happens once row has changed user think saving new row , not current row.

i seem caught in circular loop how has messagebox box getting fired numerious times setting isdirty true if user enters key down on textboxes.

if (isdirty == true) {             dialogresult _result = messagebox.show("are sure wish upate live product information", "save changes", messageboxbuttons.yesno, messageboxicon.information);             if (_result == dialogresult.yes)             {                 updatestock();                 _trackchanges.clear();                  isdirty = false;             }  } 

this me setting dirty flag on key down felt best way avoid problem seem having.

private void txtdescription_keydown(object sender, keyeventargs e) {         isdirty = true;         btnsavedetails.enabled = true;  } 

your code sample suggests save stuff defined in save button click. if change structure this

private void saveclick(object sender, eventargs e) {     dosavestuff(); }  private void dosavestuff() {     // save stuff } 

i.e. pull save stuff out method. can call dosavestuff() whenever need save, e.g.

private void selectionchanged(object sender, eventargs e) {     // stuff      if (condition)     {         dosavestuff();     } } 

the advantage of approach you're capturing aspect of behaviour you're interested in - save stuff - rather whole button click. furthermore, click button save, application doesn't, saves in circumstances, e.g. when click button or when changes.

as msdn says selectionchangedevent occurs whenever selection has changed. so, whenever happens check original selection see if has changed , save if has. maybe doing this

private void selectionchanged(object sender, eventargs e) {     bool hascontentschanged = // determine if content has changed      if (hascontentschanged)     {         dosavestuff();     } } 

an advantage of approach you'd have save if had changed, i.e. if original text != new text, rather in cases.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -