private void gridDepartment_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e) { if (e.EditAction == GridViewEditAction.Cancel) { return; } if (e.EditOperationType == GridViewEditOperationType.Insert) { try { db.SaveChanges(); binddatagrid(); } catch { MessageBox.Show("already exist"); } } else if (e.EditOperationType == GridViewEditOperationType.Edit) { try { foreach (var data in gridDepartment.SelectedItems) { t_Department myData = data as t_Department; deptcode = myData.DeptCode; depdesc = myData.DeptName; t_Department dept = db.t_Department.First(p => p.DeptCode == deptcode); dept.DeptName = depdesc; db.SaveChanges(); binddatagrid(); } } catch { } } }
private void gridDepartment_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e) { var dept = new t_Department(); dept.DeptCode = deptcode; dept.DeptName = depdesc; e.NewObject = dept; db.t_Department.Add(dept); }
private void gridDepartment_PreviewKeyDown(object sender, KeyEventArgs e) { try { if (e.Key == Key.Delete) { string sMessageBoxText = "Do you want to continue?"; string sCaption = "Delete Department"; MessageBoxButton btnMessageBox = MessageBoxButton.YesNo; MessageBoxImage icnMessageBox = MessageBoxImage.Warning; MessageBoxResult rsltMessageBox = MessageBox.Show(sMessageBoxText, sCaption, btnMessageBox, icnMessageBox); switch (rsltMessageBox) { case MessageBoxResult.Yes: foreach (var data in gridDepartment.SelectedItems) { t_Department myData = data as t_Department; var deleteuser = db.t_Department.Where(DeptCode => DeptCode.DeptCode == myData.DeptCode).FirstOrDefault(); db.t_Department.Remove(deleteuser); db.SaveChanges(); } break; case MessageBoxResult.No: /* ... */ break; } } else { return; } } catch { } }