Index: Core/Common/src/Core.Common.Controls/DataGrid/DataGridViewControl.cs =================================================================== diff -u -rf1be6965092b5dc1b0e2e145d59e718493351fb8 -r0efc76f9c5ce1c3c26e501bbd2e884e2fcfa64ed --- Core/Common/src/Core.Common.Controls/DataGrid/DataGridViewControl.cs (.../DataGridViewControl.cs) (revision f1be6965092b5dc1b0e2e145d59e718493351fb8) +++ Core/Common/src/Core.Common.Controls/DataGrid/DataGridViewControl.cs (.../DataGridViewControl.cs) (revision 0efc76f9c5ce1c3c26e501bbd2e884e2fcfa64ed) @@ -19,6 +19,7 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System; using System.Windows.Forms; namespace Core.Common.Controls.DataGrid @@ -34,8 +35,15 @@ public DataGridViewControl() { InitializeComponent(); + + SubscribeEvents(); } + private void SubscribeEvents() + { + dataGridView.CurrentCellDirtyStateChanged += DataGridViewCurrentCellDirtyStateChanged; + } + /// /// Adds a new to the with the given data. /// @@ -54,5 +62,36 @@ ReadOnly = readOnly }); } + + /// + /// Adds a new to the with the given data. + /// + /// The of the column. + /// The of the column. + /// is also used to create the . + /// The format is "column_. + public void AddCheckBoxColumn(string dataPropertyName, string headerText) + { + dataGridView.Columns.Add(new DataGridViewCheckBoxColumn + { + DataPropertyName = dataPropertyName, + HeaderText = headerText, + Name = string.Format("column_{0}", dataPropertyName) + }); + } + + #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); + } + } + + #endregion } } Index: Core/Common/test/Core.Common.Controls.Test/DataGrid/DataGridViewControlTest.cs =================================================================== diff -u -rf1be6965092b5dc1b0e2e145d59e718493351fb8 -r0efc76f9c5ce1c3c26e501bbd2e884e2fcfa64ed --- Core/Common/test/Core.Common.Controls.Test/DataGrid/DataGridViewControlTest.cs (.../DataGridViewControlTest.cs) (revision f1be6965092b5dc1b0e2e145d59e718493351fb8) +++ Core/Common/test/Core.Common.Controls.Test/DataGrid/DataGridViewControlTest.cs (.../DataGridViewControlTest.cs) (revision 0efc76f9c5ce1c3c26e501bbd2e884e2fcfa64ed) @@ -19,6 +19,7 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System; using System.Windows.Forms; using Core.Common.Controls.DataGrid; using NUnit.Extensions.Forms; @@ -100,11 +101,77 @@ Assert.AreEqual(1, dataGridView.ColumnCount); var columnData = dataGridView.Columns[0]; + Assert.IsTrue(columnData.GetType() == typeof(DataGridViewTextBoxColumn)); Assert.AreEqual(propertyName, columnData.DataPropertyName); Assert.AreEqual(string.Format("column_{0}", propertyName), columnData.Name); Assert.AreEqual(headerText, columnData.HeaderText); Assert.AreEqual(readOnly, columnData.ReadOnly); } } + + [Test] + public void AddCheckBoxColumn_Always_AddsColumnToDataGridView() + { + // Setup + using (var form = new Form()) + { + var propertyName = "PropertyName"; + var headerText = "HeaderText"; + + var control = new DataGridViewControl(); + form.Controls.Add(control); + form.Show(); + + var dataGridView = (DataGridView)new ControlTester("dataGridView").TheObject; + + // Precondition + Assert.AreEqual(0, dataGridView.ColumnCount); + + // Call + control.AddCheckBoxColumn(propertyName, headerText); + + // Assert + Assert.AreEqual(1, dataGridView.ColumnCount); + + var columnData = dataGridView.Columns[0]; + Assert.IsTrue(columnData.GetType() == typeof(DataGridViewCheckBoxColumn)); + Assert.AreEqual(propertyName, columnData.DataPropertyName); + Assert.AreEqual(string.Format("column_{0}", propertyName), columnData.Name); + Assert.AreEqual(headerText, columnData.HeaderText); + } + } + + [Test] + public void DataGridViewControl_EditValueDirtyStateChangedEventFired_ValueCommittedCellInEditMode() + { + // Setup + using (var form = new Form()) + { + var control = new DataGridViewControl(); + form.Controls.Add(control); + form.Show(); + + var gridTester = new ControlTester("dataGridView"); + var dataGridView = (DataGridView)gridTester.TheObject; + + control.AddCheckBoxColumn("Test property", "Test header"); + + // Precondition + Assert.AreEqual(1, dataGridView.ColumnCount); + + dataGridView.DataSource = new[] { false }; + + var dataGridViewCell = dataGridView.Rows[0].Cells[0]; + dataGridView.CurrentCell = dataGridViewCell; + dataGridView.BeginEdit(false); + gridTester.FireEvent("KeyUp", new KeyEventArgs(Keys.Space)); + + // Call + gridTester.FireEvent("CurrentCellDirtyStateChanged", EventArgs.Empty); + + // Assert + Assert.IsTrue(dataGridViewCell.IsInEditMode); + } + } } } \ No newline at end of file