Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. //This is the simplest way from me to do this by db.BeginTransaction on Form_Load
  2.  
  3. private void Form1_Load(object sender, EventArgs e)
  4. {
  5. studentBindingSource.DataSource = db.Query<student>("SELECT * FROM student");
  6. db.BeginTransaction(); // Begin Transaction here
  7. }
  8.  
  9. //On Update (CellValueChanged) just simply do db.Save() this doesn't commit yet because our transaction is not complete yet
  10.  
  11. private void studentDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  12. {
  13. try
  14. {
  15. db.Save((student)studentBindingSource.Current);
  16. }
  17. catch (Exception)
  18. {
  19. return;
  20. }
  21. }
  22. //And yes we can do Insert and Delete as well !!!
  23.  
  24. //Insert
  25.  
  26. private void buttonInsert_Click(object sender, EventArgs e)
  27. {
  28. student newStudent = new student
  29. {
  30. StudentName = "POCONEW"
  31. }
  32. studentBindingSource.Add(newStudent);
  33. db.Save(newStudent);
  34. }
  35.  
  36. //Delete
  37.  
  38. private void studentDataGridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
  39. {
  40. int rowIndex = e.Row.Index; // We use Gridview row index instead because BindingSource.Current isn't work when user drag then deleting multiple row
  41. db.Delete(studentDataGridView.Rows[rowIndex].DataBoundItem as student);
  42. }
  43.  
  44. //Finally to save change we just do db.CompleteTransaction() :Just a little note here after user press the Save Button if you don't close the form you have to call db.BeginTransaction() again if not everything user edit after this will be saved automatically because we don't have Transaction anymore
  45.  
  46. private void btSave_Click(object sender, EventArgs e)
  47. {
  48. db.CompleteTransaction();
  49. db.BeginTransaction(); // everything user edit after this will be saved automatically because we don't have Transaction anymore so we Begin it again
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement