Index: Ringtoets/Common/src/Ringtoets.Common.Forms/Views/FailureMechanismResultView.cs =================================================================== diff -u -r0d1a1a5d6962e334a56ec7fd0c83488c6f377ca3 -ra048dc4662f1eb83e4e8cd127790473f01470263 --- Ringtoets/Common/src/Ringtoets.Common.Forms/Views/FailureMechanismResultView.cs (.../FailureMechanismResultView.cs) (revision 0d1a1a5d6962e334a56ec7fd0c83488c6f377ca3) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/Views/FailureMechanismResultView.cs (.../FailureMechanismResultView.cs) (revision a048dc4662f1eb83e4e8cd127790473f01470263) @@ -19,12 +19,11 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. -using System; using System.Collections.Generic; -using System.Drawing; using System.Linq; using System.Windows.Forms; using Core.Common.Base; +using Core.Common.Controls.DataGrid; using Core.Common.Controls.Views; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Forms.Properties; @@ -38,9 +37,9 @@ /// public abstract partial class FailureMechanismResultView : UserControl, IView where T : FailureMechanismSectionResult { + private const int assessmentLayerOneColumnIndex = 1; private readonly IList failureMechanismSectionResultObservers; private readonly Observer failureMechanismObserver; - private const int assessmentLayerOneColumnIndex = 1; private IEnumerable failureMechanismSectionResult; private IFailureMechanism failureMechanism; @@ -85,11 +84,13 @@ } else { - dataGridView.DataSource = null; + DataGridViewControl.SetDataSource(null); } } } + protected DataGridViewControl DataGridViewControl { get; private set; } + protected override void Dispose(bool disposing) { FailureMechanism = null; @@ -108,85 +109,33 @@ /// /// Finds out whether the assessment section which is represented by the row at index - /// has passed the level 0 assessment. + /// has passed the level 1 assessment. /// /// The index of the row which has a section attached. - /// false if assessment level 0 has passed, true otherwise. - protected bool HasPassedLevelZero(int rowIndex) + /// false if assessment level 1 has passed, true otherwise. + protected bool HasPassedLevelOne(int rowIndex) { - var row = dataGridView.Rows[rowIndex]; - return (bool) row.Cells[assessmentLayerOneColumnIndex].Value; + return (bool) DataGridViewControl.GetCell(rowIndex, assessmentLayerOneColumnIndex).Value; } /// - /// Add a handler for the event. - /// - /// The handler to add. - protected void AddCellFormattingHandler(DataGridViewCellFormattingEventHandler handler) - { - dataGridView.CellFormatting += handler; - } - - /// - /// Restore the initial style of the cell at , . - /// - /// The row index of the cell. - /// The column index of the cell. - protected void RestoreCell(int rowIndex, int columnIndex) - { - var cell = dataGridView.Rows[rowIndex].Cells[columnIndex]; - cell.ReadOnly = GetDataGridColumns().ElementAt(columnIndex).ReadOnly; - SetCellStyle(cell, CellStyle.Enabled); - } - - /// - /// Gives the cell at , a - /// disabled style. - /// - /// The row index of the cell. - /// The column index of the cell. - protected void DisableCell(int rowIndex, int columnIndex) - { - var cell = GetCell(rowIndex, columnIndex); - cell.ReadOnly = true; - SetCellStyle(cell, CellStyle.Disabled); - } - - protected DataGridViewCell GetCell(int rowIndex, int columnIndex) - { - return dataGridView.Rows[rowIndex].Cells[columnIndex]; - } - - /// /// Gets all the columns that should be added to the on the /// . /// /// An of . - protected virtual IEnumerable GetDataGridColumns() + protected virtual void AddDataGridColumns() { - yield return new DataGridViewTextBoxColumn - { - DataPropertyName = "Name", - HeaderText = Resources.FailureMechanismResultView_InitializeDataGridView_Section_name, - Name = "column_Name", - ReadOnly = true - }; - - yield return new DataGridViewCheckBoxColumn - { - DataPropertyName = "AssessmentLayerOne", - HeaderText = Resources.FailureMechanismResultView_InitializeDataGridView_Assessment_layer_one, - Name = "column_AssessmentLayerOne" - }; + DataGridViewControl.AddTextBoxColumn("Name", Resources.FailureMechanismResultView_InitializeDataGridView_Section_name, true); + DataGridViewControl.AddCheckBoxColumn("AssessmentLayerOne", Resources.FailureMechanismResultView_InitializeDataGridView_Assessment_layer_one); } /// /// Updates the data source of the data grid view with the current known failure mechanism section results. /// protected void UpdataDataGridViewDataSource() { - EndEdit(); - dataGridView.DataSource = failureMechanismSectionResult.Select(CreateFailureMechanismSectionResultRow).ToList(); + DataGridViewControl.EndEdit(); + DataGridViewControl.SetDataSource(failureMechanismSectionResult.Select(CreateFailureMechanismSectionResultRow).ToList()); } /// @@ -196,7 +145,7 @@ /// The data bound to the row at index . protected object GetDataAtRow(int rowIndex) { - return dataGridView.Rows[rowIndex].DataBoundItem; + return DataGridViewControl.GetRowFromIndex(rowIndex).DataBoundItem; } /// @@ -222,23 +171,11 @@ } } - private void SetCellStyle(DataGridViewCell cell, CellStyle style) - { - cell.Style.BackColor = style.BackgroundColor; - cell.Style.ForeColor = style.TextColor; - } - - private void RefreshDataGridView() - { - dataGridView.Refresh(); - dataGridView.AutoResizeColumns(); - } - private void AddSectionResultObservers() { foreach (var sectionResult in failureMechanismSectionResult) { - failureMechanismSectionResultObservers.Add(new Observer(RefreshDataGridView) + failureMechanismSectionResultObservers.Add(new Observer(DataGridViewControl.RefreshDataGridView) { Observable = sectionResult }); @@ -256,81 +193,7 @@ private void InitializeDataGridView() { - dataGridView.GotFocus += DataGridViewGotFocus; - dataGridView.AutoGenerateColumns = false; - dataGridView.Columns.AddRange(GetDataGridColumns().ToArray()); + AddDataGridColumns(); } - - private void EndEdit() - { - if (dataGridView.IsCurrentCellInEditMode) - { - dataGridView.CancelEdit(); - dataGridView.EndEdit(); - dataGridView.CurrentCell = null; - } - } - - private class CellStyle - { - public static readonly CellStyle Enabled = new CellStyle - { - TextColor = Color.FromKnownColor(KnownColor.ControlText), - BackgroundColor = Color.FromKnownColor(KnownColor.White) - }; - - public static readonly CellStyle Disabled = new CellStyle - { - TextColor = Color.FromKnownColor(KnownColor.GrayText), - BackgroundColor = Color.FromKnownColor(KnownColor.DarkGray) - }; - - public Color TextColor { get; private set; } - public Color BackgroundColor { get; private set; } - } - - #region Event handling - - private void DataGridViewCurrentCellDirtyStateChanged(object sender, EventArgs e) - { - // Ensure checkbox values are directly committed - DataGridViewColumn currentColumn = dataGridView.Columns[dataGridView.CurrentCell.ColumnIndex]; - if (currentColumn is DataGridViewCheckBoxColumn) - { - dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit); - } - } - - private void DataGridViewCellValidating(object sender, DataGridViewCellValidatingEventArgs e) - { - dataGridView.Rows[e.RowIndex].ErrorText = String.Empty; - - var cellEditValue = e.FormattedValue.ToString(); - if (string.IsNullOrWhiteSpace(cellEditValue)) - { - dataGridView.Rows[e.RowIndex].ErrorText = CoreCommonControlsResources.DataGridViewCellValidating_Text_may_not_be_empty; - } - } - - private void DataGridViewDataError(object sender, DataGridViewDataErrorEventArgs e) - { - e.ThrowException = false; - e.Cancel = true; - - if (string.IsNullOrWhiteSpace(dataGridView.Rows[e.RowIndex].ErrorText) && e.Exception != null) - { - dataGridView.Rows[e.RowIndex].ErrorText = e.Exception.Message; - } - } - - private void DataGridViewGotFocus(object sender, EventArgs eventArgs) - { - if (dataGridView.CurrentCell != null) - { - dataGridView.BeginEdit(true); // Always start editing after setting the focus (otherwise data grid view cell dirty events are no longer fired when using the keyboard...) - } - } - - #endregion } } \ No newline at end of file