diff options
author | sancheolz <sancheolz@gmail.com> | 2019-05-06 16:11:49 +0300 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2019-05-06 16:11:49 +0300 |
commit | 3e51e7d222cf50dc34d91e5e10620c230cd09d3d (patch) | |
tree | bba172af9798e2afd6ba60977d35018c6ed8ed15 /mcs/class/System.Windows.Forms | |
parent | b6a652c072ecc6a0fdc8f0e9e5ea581b590af674 (diff) |
[WinForms] fix EditingCellFormattedValue getter and setter for bool value (#13845)
* [WinForms] If DataGridViewCheckBoxCell not in threeState mode
EditingCellFormattedValue must return true or false, instead of
value of CheckState type
* [WinForms] DataGrid: fix type check in EditingCellFormattedValue setter.
When set bool value to EditingCellFormattedValue there will be exception.
Type equality must be done through IsAssignableFrom not direct
* [WinForms] DataGrid: Add test for EditingCellFormattedValue getter and setter.
Diffstat (limited to 'mcs/class/System.Windows.Forms')
-rw-r--r-- | mcs/class/System.Windows.Forms/System.Windows.Forms/DataGridViewCheckBoxCell.cs | 11 | ||||
-rw-r--r-- | mcs/class/System.Windows.Forms/Test/System.Windows.Forms/DataGridViewCheckBoxCellTest.cs | 30 |
2 files changed, 38 insertions, 3 deletions
diff --git a/mcs/class/System.Windows.Forms/System.Windows.Forms/DataGridViewCheckBoxCell.cs b/mcs/class/System.Windows.Forms/System.Windows.Forms/DataGridViewCheckBoxCell.cs index 6ef788eff1f..c88624de5c6 100644 --- a/mcs/class/System.Windows.Forms/System.Windows.Forms/DataGridViewCheckBoxCell.cs +++ b/mcs/class/System.Windows.Forms/System.Windows.Forms/DataGridViewCheckBoxCell.cs @@ -59,13 +59,14 @@ namespace System.Windows.Forms { public DataGridViewCheckBoxCell (bool threeState) : this() { this.threeState = threeState; - editingCellFormattedValue = CheckState.Unchecked; + if (threeState) + editingCellFormattedValue = CheckState.Unchecked; } public virtual object EditingCellFormattedValue { get { return editingCellFormattedValue; } set { - if (FormattedValueType == null || value == null || value.GetType() != FormattedValueType || !(value is Boolean) || !(value is CheckState)) { + if (FormattedValueType == null || value == null || !FormattedValueType.IsAssignableFrom(value.GetType())) { throw new ArgumentException("Cannot set this property."); } editingCellFormattedValue = value; @@ -192,7 +193,11 @@ namespace System.Windows.Forms { public virtual void PrepareEditingCellForEdit (bool selectAll) { - editingCellFormattedValue = GetCurrentValue (); + CheckState cs = GetCurrentValue(); + if (threeState) + editingCellFormattedValue = cs; + else + editingCellFormattedValue = cs == CheckState.Checked; } public override string ToString () diff --git a/mcs/class/System.Windows.Forms/Test/System.Windows.Forms/DataGridViewCheckBoxCellTest.cs b/mcs/class/System.Windows.Forms/Test/System.Windows.Forms/DataGridViewCheckBoxCellTest.cs index 7b2f090038c..1ad4e69ef24 100644 --- a/mcs/class/System.Windows.Forms/Test/System.Windows.Forms/DataGridViewCheckBoxCellTest.cs +++ b/mcs/class/System.Windows.Forms/Test/System.Windows.Forms/DataGridViewCheckBoxCellTest.cs @@ -200,6 +200,36 @@ namespace MonoTests.System.Windows.Forms } [Test] + public void EditingCellFormattedValue() + { + var boolCheckBoxCell = new DataGridViewCheckBoxCell(); + Assert.AreEqual(false, boolCheckBoxCell.EditingCellFormattedValue, "A1"); + boolCheckBoxCell.EditingCellFormattedValue = true; + Assert.AreEqual(true, boolCheckBoxCell.EditingCellFormattedValue, "A2"); + + var treeStateCheckBoxCell = new DataGridViewCheckBoxCell(true); + Assert.AreEqual(CheckState.Unchecked, treeStateCheckBoxCell.EditingCellFormattedValue, "A3"); + treeStateCheckBoxCell.EditingCellFormattedValue = CheckState.Checked; + Assert.AreEqual(CheckState.Checked, treeStateCheckBoxCell.EditingCellFormattedValue, "A4"); + } + + [Test] + [ExpectedException(typeof(ArgumentException))] + public void BoolEditingCellFormattedValueCheckStateSet() + { + var boolCheckBoxCell = new DataGridViewCheckBoxCell(); + boolCheckBoxCell.EditingCellFormattedValue = CheckState.Checked; + } + + [Test] + [ExpectedException(typeof(ArgumentException))] + public void TreeStateEditingCellFormattedValueBoolSet() + { + var treeStateCheckBoxCell = new DataGridViewCheckBoxCell(true); + treeStateCheckBoxCell.EditingCellFormattedValue = false; + } + + [Test] public void FormattedValueType () { BaseCell c = new BaseCell (); |