private void InputChooseLoadData_SelectedIndexChanged(object sender, EventArgs e) { if (InputChooseLoadData.SelectedIndex < 1 || mIsUpdating) { return; } using (var context = new mydbEntities()) { string selectedReportID = InputChooseLoadData.SelectedItem.ToString().Substring(4, InputChooseLoadData.SelectedItem.ToString().IndexOf(" --- ") - (" --- ").Length + 1); mMotorTestData = context.Motor_test_data.FirstOrDefault(t => t.InputReportID == selectedReportID); if (null == mMotorTestData) { return; } motortestdataBindingSource.DataSource = mMotorTestData; if (!mIsUpdating) { ReloadComboboxContentFromDatabase(); ForceRefreshBindingDataToControls(); } //InputChooseModel.SelectedIndex = mMotorTestData.ID_Motor; } }
private void ReloadComboboxContentFromDatabase() { if (mIsUpdating) { return; } mIsUpdating = true; using (var context = new mydbEntities()) { var moterList = context.Motors.Where(m => m.Series == InputChooseSeries.Text); InputChooseModel.Items.Clear(); InputChooseModel.Items.Add(""); foreach (Motor motor in moterList) { InputChooseModel.Items.Add(motor.Desc); //System.Console.WriteLine(motor.ID); } var motorTestDataList = context.Motor_test_data.ToList(); InputChooseLoadData.Items.Clear(); InputChooseLoadData.Items.Add(""); foreach (Motor_test_data motorTestData in motorTestDataList) { InputChooseLoadData.Items.Add("ID: " + motorTestData.InputReportID + " --- " + motorTestData.InputMotorSN + " --- " + motorTestData.InputTestDate);//Report ID } InputChooseModel.SelectedItem = mMotorTestData.InputChooseModel; InputChooseLoadData.SelectedItem = "ID: " + mMotorTestData.InputReportID + " --- " + mMotorTestData.InputMotorSN + " --- " + mMotorTestData.InputTestDate;//Report ID } mIsUpdating = false; }
private void InputChooseModel_SelectedIndexChanged(object sender, EventArgs e) { if (InputChooseModel.SelectedIndex < 1 || mIsUpdating) { return; } using (var context = new mydbEntities()) { mMotorTestData.Motor = context.Motors.FirstOrDefault(m => m.Desc == InputChooseModel.Text); if (null == mMotorTestData.Motor) { return; } //mMotorTestData.ID_Motor = mMotorTestData.motor.ID; mMotorTestData.InputChooseModel = mMotorTestData.Motor.Desc; mMotorTestData.OutputInsulationResistanceCheckMinVoltRequired = (mMotorTestData.Motor.Volts < 1000M) ? 500M : ((mMotorTestData.Motor.Volts < 2500M) ? 1000M : ((mMotorTestData.Motor.Volts < 5000M) ? 2500M : 0M)); mMotorTestData.OutputRecommendedLowVoltStartAt60Hz = 0.4M * mMotorTestData.Motor.Volts; } ForceRefreshBindingDataToControls(); }
private bool ReCalculateOutputs(string operation = "") { using (var context = new mydbEntities()) { mMotorTestData.Motor = context.Motors.FirstOrDefault(m => m.Desc == InputChooseModel.Text); mMotorTestData.Motor_shaft_extension = context.Motor_shaft_extension.FirstOrDefault(mse => mse.Desc.Contains(mMotorTestData.InputChooseSeries) && mse.Desc.Contains(mMotorTestData.InputChooseMotorConfigForShaftExtension)); //if (null == mMotorTestData.motor || null == mMotorTestData.Motor_shaft_extension) // return false; if (null != mMotorTestData.Motor) { mMotorTestData.ID_Motor = mMotorTestData.Motor.ID; } if (null != mMotorTestData.Motor_shaft_extension) { mMotorTestData.ID_Motor_Shaft_Extension = mMotorTestData.Motor_shaft_extension.ID; } mMotorTestData.ReCalculateOutputs(); if ("" != operation) { if (null == mMotorTestData.InputReportID || "" == mMotorTestData.InputReportID) { MessageBox.Show("Please enter a Report ID to continue", operation + " Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(false); } if (mMotorTestData.ID_Motor < 1 || mMotorTestData.ID_Motor_Shaft_Extension < 1) { MessageBox.Show("Please choose a Motor Model to continue", operation + " Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(false); } if (null != context.Motor_test_data.FirstOrDefault(mt => mt.InputReportID == mMotorTestData.InputReportID)) { MessageBox.Show("Entered Report ID already exists, please try another one", operation + " Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(false); } } if ("Save" == operation) { context.Motor_test_data.Add(mMotorTestData); if (context.SaveChanges() > 0) { System.Console.WriteLine($"Save new record to table Motor_test_data, ID: { mMotorTestData.ID }"); } else { System.Console.WriteLine($"Failed on saving new record ID: { mMotorTestData.ID }"); return(false); } } else if ("Update" == operation) { var preTestData = context.Motor_test_data.FirstOrDefault(mtd => mtd.ID == mMotorTestData.ID); if (null == preTestData) { System.Console.WriteLine($"Unable to locate ID: { mMotorTestData.ID } in test record"); return(false); } context.Entry(preTestData).CurrentValues.SetValues(mMotorTestData); if (context.SaveChanges() > 0) { System.Console.WriteLine($"Update record in table Motor_test_data, ID = { mMotorTestData.ID }"); } else { System.Console.WriteLine($"Failed on updating record ID: { mMotorTestData.ID }"); return(false); } } } return(true); }