public void Save() { //Get an instance of the service using Helper class var client = Helper.getServiceClient(); try { //Get the current object in the EditGrid var LastAccountInfoToSave = (FIN_AccountType)CollectionViewSource.GetDefaultView(account.AccountTypeGrid.DataContext).CurrentItem; //This is used incase a user never enters a textbox but still needs validating account.txt_AccountType_AccountType .GetBindingExpression(TextBox.TextProperty).UpdateSource(); account.txt_AccountType_AccountDesc.GetBindingExpression(TextBox.TextProperty).UpdateSource(); //Checking if all controls are in valid state if (!Helper.IsValid(account.AccountTypeGrid)) { MessageBox.Show("Please fix errors before continuing", "Message", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } //A user is trying to add a record here if (account.BtnAdd.IsChecked == true) { var ltitosave = (LastAccountInfoToSave); if (!client.AccountTypeExists(ltitosave)) { ApiAck ack = client.CreateAccountType(ltitosave); if (ack.CallStatus == EApiCallStatus.Success) { MessageBox.Show("Record Added Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information); //Complete the Add operation. i.e.; Re-enable tabs,textboxes and datagrid CompleteAdd(); } else { MessageBox.Show(ack.ReturnedMessage, "Message", MessageBoxButton.OK, MessageBoxImage.Exclamation); } } else { MessageBox.Show("Record Already Exists", "Message", MessageBoxButton.OK, MessageBoxImage.Exclamation); } } //Else the user is trying to update a record else { ApiAck ack = client.UpdateAccountType(LastAccountInfoToSave); if (ack.CallStatus == EApiCallStatus.Success) { MessageBox.Show("Record Updated Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information); CompleteUpdate(); } else { MessageBox.Show(ack.ReturnedMessage, "Message", MessageBoxButton.OK, MessageBoxImage.Exclamation); } } } catch (NullReferenceException) { MessageBox.Show("Nothing to Update", "Message", MessageBoxButton.OK, MessageBoxImage.Exclamation); } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButton.OK, MessageBoxImage.Exclamation); } }
//Stop generating columns for fields which don't have the [DataMember] attribute;See entity.cs //This is used to hide fields like EnteredUser,EnteredData etc; void dgv_AccountType_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (!Attribute.IsDefined(typeof(FIN_AccountType).GetProperty(e.PropertyName), typeof(DataMemberAttribute))) e.Cancel = true; e.Column.Header = Helper.FormatHeaders(e.PropertyName, false); }