Skip to content

Commit 4fa246d

Browse files
monojenkinsakoeplinger
authored andcommitted
[2019-02] [WinForms] fix EditingCellFormattedValue getter and setter for bool value (#14437)
* [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.
1 parent a55d1c9 commit 4fa246d

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

mcs/class/System.Windows.Forms/System.Windows.Forms/DataGridViewCheckBoxCell.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ public DataGridViewCheckBoxCell ()
5959
public DataGridViewCheckBoxCell (bool threeState) : this()
6060
{
6161
this.threeState = threeState;
62-
editingCellFormattedValue = CheckState.Unchecked;
62+
if (threeState)
63+
editingCellFormattedValue = CheckState.Unchecked;
6364
}
6465

6566
public virtual object EditingCellFormattedValue {
6667
get { return editingCellFormattedValue; }
6768
set {
68-
if (FormattedValueType == null || value == null || value.GetType() != FormattedValueType || !(value is Boolean) || !(value is CheckState)) {
69+
if (FormattedValueType == null || value == null || !FormattedValueType.IsAssignableFrom(value.GetType())) {
6970
throw new ArgumentException("Cannot set this property.");
7071
}
7172
editingCellFormattedValue = value;
@@ -192,7 +193,11 @@ public override object ParseFormattedValue (object formattedValue, DataGridViewC
192193

193194
public virtual void PrepareEditingCellForEdit (bool selectAll)
194195
{
195-
editingCellFormattedValue = GetCurrentValue ();
196+
CheckState cs = GetCurrentValue();
197+
if (threeState)
198+
editingCellFormattedValue = cs;
199+
else
200+
editingCellFormattedValue = cs == CheckState.Checked;
196201
}
197202

198203
public override string ToString ()

mcs/class/System.Windows.Forms/Test/System.Windows.Forms/DataGridViewCheckBoxCellTest.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,36 @@ public void EditedFormattedValue ()
199199
Assert.AreEqual (null, c.EditedFormattedValue, "A1");
200200
}
201201

202+
[Test]
203+
public void EditingCellFormattedValue()
204+
{
205+
var boolCheckBoxCell = new DataGridViewCheckBoxCell();
206+
Assert.AreEqual(false, boolCheckBoxCell.EditingCellFormattedValue, "A1");
207+
boolCheckBoxCell.EditingCellFormattedValue = true;
208+
Assert.AreEqual(true, boolCheckBoxCell.EditingCellFormattedValue, "A2");
209+
210+
var treeStateCheckBoxCell = new DataGridViewCheckBoxCell(true);
211+
Assert.AreEqual(CheckState.Unchecked, treeStateCheckBoxCell.EditingCellFormattedValue, "A3");
212+
treeStateCheckBoxCell.EditingCellFormattedValue = CheckState.Checked;
213+
Assert.AreEqual(CheckState.Checked, treeStateCheckBoxCell.EditingCellFormattedValue, "A4");
214+
}
215+
216+
[Test]
217+
[ExpectedException(typeof(ArgumentException))]
218+
public void BoolEditingCellFormattedValueCheckStateSet()
219+
{
220+
var boolCheckBoxCell = new DataGridViewCheckBoxCell();
221+
boolCheckBoxCell.EditingCellFormattedValue = CheckState.Checked;
222+
}
223+
224+
[Test]
225+
[ExpectedException(typeof(ArgumentException))]
226+
public void TreeStateEditingCellFormattedValueBoolSet()
227+
{
228+
var treeStateCheckBoxCell = new DataGridViewCheckBoxCell(true);
229+
treeStateCheckBoxCell.EditingCellFormattedValue = false;
230+
}
231+
202232
[Test]
203233
public void FormattedValueType ()
204234
{

0 commit comments

Comments
 (0)