//private void dgvPlantLocations_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) //{ // if (e.RowIndex < mLstPlantLocations.Count - 1) // { // this.dgvPlantLocations.Rows[e.RowIndex].Tag = mLstPlantLocations[e.RowIndex]; // } //} //private void dgvColonyLocations_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) //{ // if (e.RowIndex < mLstColonyLocations.Count - 1) // { // this.dgvColonyLocations.Rows[e.RowIndex].Tag = mLstColonyLocations[e.RowIndex]; // } //} //private void dgvPlantLocations_UserAddedRow(object sender, DataGridViewRowEventArgs e) //{ // try // { // string newValue = e.Row.Cells[1].Value as string; // VisitingLocations newLoacation = new VisitingLocations() // { // IsOnPlant = true, // Location = newValue // }; // EFERTDbUtility.mEFERTDb.VisitingLocations.Add(newLoacation); // EFERTDbUtility.mEFERTDb.SaveChanges(); // EFERTDbUtility.mVisitingLocations.Add(newLoacation); // e.Row.Tag = newLoacation; // } // catch (Exception ex) // { // MessageBox.Show(this, "Some error occurred in deleting location.\n\n" + EFERTDbUtility.GetInnerExceptionMessage(ex)); // } //} private void dgvPlantLocations_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { try { VisitingLocations visitingLocations = e.Row.Tag as VisitingLocations; EFERTDbUtility.mEFERTDb.Entry(visitingLocations).State = System.Data.Entity.EntityState.Deleted; EFERTDbUtility.mEFERTDb.SaveChanges(); EFERTDbUtility.mVisitingLocations.Remove(visitingLocations); } catch (Exception ex) { MessageBox.Show(this, "Some error occurred in deleting location.\n\n" + EFERTDbUtility.GetInnerExceptionMessage(ex)); e.Cancel = true; } }
private void dgvColonyLocations_CellEndEdit(object sender, DataGridViewCellEventArgs e) { DataGridViewRow row = this.dgvColonyLocations.Rows[e.RowIndex]; string location = row.Cells[1].Value as string; if (row == null || string.IsNullOrEmpty(location)) { this.dgvColonyLocations.CancelEdit(); return; } if (!string.IsNullOrEmpty(location)) { location = location.Trim().ToLower(); } List <VisitingLocations> visitingLocations = EFERTDbUtility.mEFERTDb.VisitingLocations.ToList(); bool locAlradyExist = visitingLocations.Exists(c => c.Location.Trim().ToLower() == location && !c.IsOnPlant); if (locAlradyExist) { this.dgvColonyLocations.CancelEdit(); return; } VisitingLocations visitingLocation = null; if (row.Tag == null) { visitingLocation = new VisitingLocations() { Location = row.Cells[1].Value as String, IsOnPlant = false }; EFERTDbUtility.mEFERTDb.VisitingLocations.Add(visitingLocation); } else { visitingLocation = row.Tag as VisitingLocations; visitingLocation.Location = row.Cells[1].Value as String; EFERTDbUtility.mEFERTDb.Entry(visitingLocation).State = System.Data.Entity.EntityState.Modified; } try { EFERTDbUtility.mEFERTDb.SaveChanges(); if (row.Tag == null) { EFERTDbUtility.mVisitingLocations.Add(visitingLocation); row.Tag = visitingLocation; } else { EFERTDbUtility.mVisitingLocations[EFERTDbUtility.mVisitingLocations.IndexOf(visitingLocation)] = visitingLocation; } } catch (Exception ex) { EFERTDbUtility.RollBack(); this.dgvColonyLocations.CancelEdit(); MessageBox.Show(this, "Some error occurred in updating visiting locations.\n\n" + EFERTDbUtility.GetInnerExceptionMessage(ex)); } }