c# - Entity Framework: List<T> Bound to DataGridView add back in deleted object to view -


how restore bound datarow (add row datagridview) after has been removed?

when click delete key on left side of datagridview userdeletingrow event fires , can capture entity object (answertable) being removed list.

private void visitorhostdatagridview_userdeletingrow (object sender, datagridviewrowcanceleventargs e)   {    if (e.row.databounditem visitorhost)     {     datatodelete datatodelete = new datatodelete (e.row, e.row.databounditem visitorhost);     visitorhostsdatarowtodelete.add (datatodelete);     setmodified (); // turns on save , cancel buttons     } } 

later delete when save happens

public override void onsave () {      if (visitorhostsdatarowtodelete.count > 0)     {         foreach (datatodelete item in visitorhostsdatarowtodelete)         {             _context.answertables.deleteobject (item.visitorhostrecord visitorhost);         }     }      visitorhostbindingsource.endedit ();      _context.savechanges ();     clearmodified (); // turns off save , cancel     messagebox.show ("saved"); } 

again, question how restore removed row (cancel delete) , add objects datagridview - after had clicked delete. (i don't need conformation, i'm talking i've done work , realized made mistake , want start over) don't want go database , shouldn't need have data, not sure how use it.

update:

i had tried list.add(entityobject) no luck (see below). tried again due comments. updated code these 3 things, in order:

this.visitorhostbindingsource.datasource = null; this.visitorhostbindingsource.datasource = _hosts; this.visitorhostsdatagridview.refresh (); 

seem solve issues having, add alone didn't make show on screen. not sure why need steps doing. additional thought useful.

public override void oncancel () // called on click event of cancel button  {   foreach (datatodelete datagridviewrow in visitorhostsdatarowtodelete)   {   _hosts.add (datagridviewrow.visitorhostrecord);   }  visitorhostsdatarowtodelete.clear();  _hosts.sort ();  this.visitorhostbindingsource.datasource = null;  this.visitorhostbindingsource.datasource = _hosts;  this.visitorhostsdatagridview.refresh ();   clearmodified (); //disable save , cancel buttons  } 

using return in event handler method exits method , not whole event handler chain. datagridview things before handler called , afterwards (where row removed bindingsource).

to state want cancel whole event have use e.cancel = true;.

so cancel stuff done after handler called write:

if(answertabledatagridview.selectedrows.count>1)     {         // multiselect false should never hit this.         messagebox.show ("only 1 delete @ time allowed,\n rows removed view not deleted database.");         e.cancel = true;         return;     } 

canceleventargs.cancel documentation


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -