private void BranchForm_FormClosing(object sender, FormClosingEventArgs e) { dgvBranch[0, 0].Selected = true; // ---------------------------------------------------------------------------------------------- // var db = new LINQDataContext(); for (int rowCounter = 0; rowCounter < dgvBranch.RowCount - 1; rowCounter++) { try { // // search dgvBranch.rows in db.Branch // int ID_No = 0; int.TryParse(dgvBranch[0, rowCounter].Value.ToString(), out ID_No); // Define the query expression. var query = (from branch in db.Branches where branch.ID_Branch == ID_No select branch).SingleOrDefault(); if (query != null) // EDIT { db.BranchEdit(ID_No, (dgvBranch[1, rowCounter].Value != null) ? (string)dgvBranch[1, rowCounter].Value.ToString() : "", (dgvBranch[2, rowCounter].Value != null) ? (string)dgvBranch[2, rowCounter].Value.ToString() : ""); } else // save { db.BranchSave(ID_No, (dgvBranch[1, rowCounter].Value != null) ? (string)dgvBranch[1, rowCounter].Value.ToString() : "", (dgvBranch[2, rowCounter].Value != null) ? (string)dgvBranch[2, rowCounter].Value.ToString() : ""); } } catch { } } db.Dispose(); }