Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is the simplest way from me to do this by db.BeginTransaction on Form_Load
- private void Form1_Load(object sender, EventArgs e)
- {
- studentBindingSource.DataSource = db.Query<student>("SELECT * FROM student");
- db.BeginTransaction(); // Begin Transaction here
- }
- //On Update (CellValueChanged) just simply do db.Save() this doesn't commit yet because our transaction is not complete yet
- private void studentDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
- {
- try
- {
- db.Save((student)studentBindingSource.Current);
- }
- catch (Exception)
- {
- return;
- }
- }
- //And yes we can do Insert and Delete as well !!!
- //Insert
- private void buttonInsert_Click(object sender, EventArgs e)
- {
- student newStudent = new student
- {
- StudentName = "POCONEW"
- }
- studentBindingSource.Add(newStudent);
- db.Save(newStudent);
- }
- //Delete
- private void studentDataGridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
- {
- int rowIndex = e.Row.Index; // We use Gridview row index instead because BindingSource.Current isn't work when user drag then deleting multiple row
- db.Delete(studentDataGridView.Rows[rowIndex].DataBoundItem as student);
- }
- //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
- private void btSave_Click(object sender, EventArgs e)
- {
- db.CompleteTransaction();
- db.BeginTransaction(); // everything user edit after this will be saved automatically because we don't have Transaction anymore so we Begin it again
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement