private void dataGridViewCompoundTreatmentElements_UserAddedRow(object sender, DataGridViewRowEventArgs e) { DataGridViewRow selectedRow = dataGridViewCompoundTreatmentElements.SelectedRows[0]; if (dataGridViewCompoundTreatmentElements.SelectedRows[0].IsNewRow) { if (selectedRow.Cells["colAttributeFrom"].Value != null && selectedRow.Cells["colAttributeTo"].Value != null && selectedRow.Cells["colExtent"].Value != null && selectedRow.Cells["colQuantity"].Value != null) { string attributeFrom = selectedRow.Cells["colAttributeFrom"].Value.ToString(); string attributeTo = selectedRow.Cells["colAttributeTo"].Value.ToString(); string extent = selectedRow.Cells["colExtent"].Value.ToString(); string quantity = selectedRow.Cells["colQuantity"].Value.ToString(); string cost = ""; string criteria = ""; if (!String.IsNullOrEmpty(selectedRow.Cells["colCost"].Value.ToString())) { cost = selectedRow.Cells["colCost"].Value.ToString(); } if (!String.IsNullOrEmpty(selectedRow.Cells["colCriteria"].Value.ToString())) { criteria = selectedRow.Cells["colCriteria"].Value.ToString(); } // Make a new compound treatment element and store it in the row. CompoundTreatmentElement createdElement = new CompoundTreatmentElement(_compoundTreatment.CompoundTreatmentID, _compoundTreatment.CompoundTreatmentName, attributeFrom, attributeTo, cost, extent, quantity, criteria); } } }
/// <summary> /// Saves any changes made to the currently selected compound treatment, including its associated elements to the database. /// </summary> private void SaveCurrentCompoundTreatmentChanges() { // Navigation off of a blank row will produce a null value for _compoundTreatment. No changes are necessary in the database in this case. if (_compoundTreatment != null) { // Statements for database changes. List <string> changeStatements = new List <string>(); // Delete from the compound treatment table our current compound treatment. This should cascade and remove its elements from teh element table as well. string delete = "DELETE FROM COMPOUND_TREATMENTS WHERE COMPOUND_TREATMENT_ID = '" + _compoundTreatment.CompoundTreatmentID + "'"; changeStatements.Add(delete); // Now insert the current compound treatment string insert = "INSERT INTO COMPOUND_TREATMENTS (COMPOUND_TREATMENT_NAME, AFFECTED_ATTRIBUTE, COMPOUND_TREATMENT_ID) VALUES ('" + _compoundTreatment.CompoundTreatmentName + "', '" + _compoundTreatment.AffectedAttribute + "', '" + _compoundTreatment.CompoundTreatmentID + "')"; changeStatements.Add(insert); foreach (DataGridViewRow elementRow in dataGridViewCompoundTreatmentElements.Rows) { CompoundTreatmentElement toInsert = (CompoundTreatmentElement)elementRow.Cells["colCompoundTreatmentElement"].Value; if (toInsert != null) { // Insert the data. insert = "INSERT INTO COMPOUND_TREATMENT_ELEMENTS (ATTRIBUTE_FROM, ATTRIBUTE_TO, EXTENT_, QUANTITY_, CRITERIA_, COST_, COMPOUND_TREATMENT_ID) VALUES ('" + toInsert.AttributeFrom + "', '" + toInsert.AttributeTo + "', '" + toInsert.ExtentEquation.m_expression + "', '" + toInsert.Quantity.m_expression + "', '" + toInsert.CriteriaEquation.Criteria + "', '" + toInsert.CostEquation.m_expression + "', '" + toInsert.CompoundTreatmentID + "')"; changeStatements.Add(insert); } } try { DBMgr.ExecuteBatchNonQuery(changeStatements); } catch (Exception exc) { SimulationMessaging.AddMessage(new SimulationMessage("Error updating database with new compound treatment values. Transaction aborted. " + exc.Message)); } } }
private void dataGridViewCompoundTreatmentElements_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { // Extent column or Quantity column if (e.ColumnIndex != -1) { // Select only one element at a time for editing if (dataGridViewCompoundTreatmentElements.SelectedCells.Count == 1) { // If the selected cell contains data if (e.RowIndex != -1) { CompoundTreatmentElement selectedCompoundElement = (CompoundTreatmentElement)dataGridViewCompoundTreatmentElements["colCompoundTreatmentElement", e.RowIndex].Value; if (selectedCompoundElement != null) { if (dataGridViewCompoundTreatmentElements.Columns["colExtent"].Index == e.ColumnIndex) { // Extent column selected FormEditEquation formEditCompoundElementEquation = new FormEditEquation(selectedCompoundElement.ExtentEquation.m_expression, _effectedTreatmentAttribute); if (formEditCompoundElementEquation.ShowDialog() == DialogResult.OK) { selectedCompoundElement.ExtentEquation.m_expression = formEditCompoundElementEquation.Equation; dataGridViewCompoundTreatmentElements[e.ColumnIndex, e.RowIndex].Value = formEditCompoundElementEquation.Equation; } } if (dataGridViewCompoundTreatmentElements.Columns["colQuantity"].Index == e.ColumnIndex) { // Quantity column selected FormEditEquation formEditCompoundElementEquation = new FormEditEquation(selectedCompoundElement.Quantity.m_expression, _effectedTreatmentAttribute); if (formEditCompoundElementEquation.ShowDialog() == DialogResult.OK) { selectedCompoundElement.Quantity.m_expression = formEditCompoundElementEquation.Equation; dataGridViewCompoundTreatmentElements[e.ColumnIndex, e.RowIndex].Value = formEditCompoundElementEquation.Equation; } } if (dataGridViewCompoundTreatmentElements.Columns["colCriteria"].Index == e.ColumnIndex) { // Criteria column selected FormAdvancedSearch formEditCriteria = new FormAdvancedSearch(selectedCompoundElement.CriteriaEquation.Criteria); if (formEditCriteria.ShowDialog() == DialogResult.OK) { selectedCompoundElement.CriteriaEquation.Criteria = formEditCriteria.GetWhereClause(); dataGridViewCompoundTreatmentElements[e.ColumnIndex, e.RowIndex].Value = formEditCriteria.GetWhereClause(); } } if (dataGridViewCompoundTreatmentElements.Columns["colCost"].Index == e.ColumnIndex) { // Cost column selected FormEditEquation formEditCompoundElementExtent = new FormEditEquation(selectedCompoundElement.CostEquation.m_expression, _effectedTreatmentAttribute); if (formEditCompoundElementExtent.ShowDialog() == DialogResult.OK) { selectedCompoundElement.CostEquation.m_expression = formEditCompoundElementExtent.Equation; dataGridViewCompoundTreatmentElements[e.ColumnIndex, e.RowIndex].Value = formEditCompoundElementExtent.Equation; } } } } else { if (dataGridViewCompoundTreatmentElements.Columns["colExtent"].Index == e.ColumnIndex) { // Extent column selected FormEditEquation formEditCompoundElementEquation = new FormEditEquation("", _effectedTreatmentAttribute); if (formEditCompoundElementEquation.ShowDialog() == DialogResult.OK) { dataGridViewCompoundTreatmentElements[e.ColumnIndex, e.RowIndex].Value = formEditCompoundElementEquation.Equation; } } if (dataGridViewCompoundTreatmentElements.Columns["colQuantity"].Index == e.ColumnIndex) { // Quantity column selected FormEditEquation formEditCompoundElementEquation = new FormEditEquation("", _effectedTreatmentAttribute); if (formEditCompoundElementEquation.ShowDialog() == DialogResult.OK) { dataGridViewCompoundTreatmentElements[e.ColumnIndex, e.RowIndex].Value = formEditCompoundElementEquation.Equation; } } if (dataGridViewCompoundTreatmentElements.Columns["colCriteria"].Index == e.ColumnIndex) { // Criteria column selected FormAdvancedSearch formEditCriteria = new FormAdvancedSearch(""); if (formEditCriteria.ShowDialog() == DialogResult.OK) { dataGridViewCompoundTreatmentElements[e.ColumnIndex, e.RowIndex].Value = formEditCriteria.GetWhereClause(); } } if (dataGridViewCompoundTreatmentElements.Columns["colCost"].Index == e.ColumnIndex) { // Cost column selected FormEditEquation formEditCompoundElementExtent = new FormEditEquation("", _effectedTreatmentAttribute); if (formEditCompoundElementExtent.ShowDialog() == DialogResult.OK) { dataGridViewCompoundTreatmentElements[e.ColumnIndex, e.RowIndex].Value = formEditCompoundElementExtent.Equation; } } } } } }