//
-#if NET_2_0
-
using System;
using System.ComponentModel;
using System.Drawing;
private object indeterminateValue;
private bool threeState;
private object trueValue;
- private Type valueType;
+// private Type valueType;
private PushButtonState check_state;
public DataGridViewCheckBoxCell ()
{
check_state = PushButtonState.Normal;
+ editingCellFormattedValue = false;
editingCellValueChanged = false;
falseValue = null;
flatStyle = FlatStyle.Standard;
indeterminateValue = null;
threeState = false;
trueValue = null;
- valueType = null;
+ ValueType = null;
}
public DataGridViewCheckBoxCell (bool threeState) : this()
{
this.threeState = threeState;
+ editingCellFormattedValue = CheckState.Unchecked;
}
public virtual object EditingCellFormattedValue {
public override Type ValueType {
get {
- if (valueType == null) {
+ if (base.ValueType == null) {
if (OwningColumn != null && OwningColumn.ValueType != null) {
return OwningColumn.ValueType;
}
}
return typeof(Boolean);
}
- return valueType;
+ return base.ValueType;
}
- set { valueType = value; }
+ set { base.ValueType = value; }
}
public override object Clone ()
{
DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell) base.Clone();
cell.editingCellValueChanged = this.editingCellValueChanged;
+ cell.editingCellFormattedValue = this.editingCellFormattedValue;
cell.falseValue = this.falseValue;
cell.flatStyle = this.flatStyle;
cell.indeterminateValue = this.indeterminateValue;
cell.threeState = this.threeState;
cell.trueValue = this.trueValue;
- cell.valueType = this.valueType;
+ cell.ValueType = this.ValueType;
return cell;
}
if ((context & DataGridViewDataErrorContexts.ClipboardContent) != 0) {
return Convert.ToString(Value);
}
- if (ThreeState) {
- return (CheckState) Value;
- }
- return (Boolean) Value;
+
+ if (editingCellFormattedValue == null)
+ if (threeState)
+ return CheckState.Indeterminate;
+ else
+ return false;
+
+ return editingCellFormattedValue;
}
public override object ParseFormattedValue (object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
public virtual void PrepareEditingCellForEdit (bool selectAll)
{
+ editingCellFormattedValue = GetCurrentValue ();
}
public override string ToString ()
protected override object GetFormattedValue (object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
- if (DataGridView == null)
- return null;
-
- if (value == falseValue)
- return false;
- if (value == trueValue)
- return true;
- if (value == DBNull.Value)
- return false;
-
- return Convert.ToBoolean (value);
+ if (DataGridView == null || value == null)
+ if (threeState)
+ return CheckState.Indeterminate;
+ else
+ return false;
+
+ return value;
}
protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
protected override void OnContentClick (DataGridViewCellEventArgs e)
{
- DataGridViewCellStyle current_style = InheritedStyle;
-
- bool current = (bool)GetFormattedValue (Value, e.RowIndex, ref current_style, null, null, DataGridViewDataErrorContexts.Parsing);
+ if (ReadOnly)
+ return;
+
+ if (!IsInEditMode)
+ DataGridView.BeginEdit (false);
- if (current)
- Value = falseValue == null ? false : falseValue;
- else
- Value = trueValue == null ? true : trueValue;
+ ToggleCheckState ();
+ }
+
+ private void ToggleCheckState ()
+ {
+ CheckState cs = GetCurrentValue ();
+
+ if (threeState) {
+ if (cs == CheckState.Indeterminate)
+ editingCellFormattedValue = CheckState.Unchecked;
+ else if (cs == CheckState.Checked)
+ editingCellFormattedValue = CheckState.Indeterminate;
+ else
+ editingCellFormattedValue = CheckState.Checked;
+ } else {
+ if (cs == CheckState.Checked)
+ editingCellFormattedValue = false;
+ else
+ editingCellFormattedValue = true;
+ }
+
+ editingCellValueChanged = true;
+ DataGridView.InvalidateCell (this);
}
protected override void OnContentDoubleClick (DataGridViewCellEventArgs e)
protected override void OnKeyDown (KeyEventArgs e, int rowIndex)
{
// when activated by the SPACE key, this method updates the cell's user interface
- if ((e.KeyData & Keys.Space) == Keys.Space) {
+ if (!ReadOnly && (e.KeyData & Keys.Space) == Keys.Space) {
check_state = PushButtonState.Pressed;
DataGridView.InvalidateCell (this);
}
protected override void OnKeyUp (KeyEventArgs e, int rowIndex)
{
// when activated by the SPACE key, this method updates the cell's user interface
- if ((e.KeyData & Keys.Space) == Keys.Space) {
+ if (!ReadOnly && (e.KeyData & Keys.Space) == Keys.Space) {
check_state = PushButtonState.Normal;
- DataGridView.InvalidateCell (this);
+ if (!IsInEditMode)
+ DataGridView.BeginEdit (false);
+ ToggleCheckState ();
}
}
protected override void OnLeave (int rowIndex, bool throughMouseClick)
{
- if (check_state != PushButtonState.Normal) {
+ if (!ReadOnly && check_state != PushButtonState.Normal) {
check_state = PushButtonState.Normal;
DataGridView.InvalidateCell (this);
}
protected override void OnMouseDown (DataGridViewCellMouseEventArgs e)
{
// if activated by depresing the left mouse button, this method updates the cell's user interface
- if ((e.Button & MouseButtons.Left) == MouseButtons.Left) {
+ if (!ReadOnly && (e.Button & MouseButtons.Left) == MouseButtons.Left) {
check_state = PushButtonState.Pressed;
DataGridView.InvalidateCell (this);
}
protected override void OnMouseLeave (int rowIndex)
{
// if the cell's button is not in its normal state, this method causes the cell's user interface to be updated.
- if (check_state != PushButtonState.Normal) {
+ if (!ReadOnly && check_state != PushButtonState.Normal) {
check_state = PushButtonState.Normal;
DataGridView.InvalidateCell (this);
}
protected override void OnMouseMove (DataGridViewCellMouseEventArgs e)
{
- if (check_state != PushButtonState.Normal && check_state != PushButtonState.Hot) {
+ if (!ReadOnly && check_state != PushButtonState.Normal && check_state != PushButtonState.Hot) {
check_state = PushButtonState.Hot;
DataGridView.InvalidateCell (this);
}
protected override void OnMouseUp (DataGridViewCellMouseEventArgs e)
{
// if activated by the left mouse button, this method updates the cell's user interface
- if ((e.Button & MouseButtons.Left) == MouseButtons.Left) {
+ if (!ReadOnly && (e.Button & MouseButtons.Left) == MouseButtons.Left) {
check_state = PushButtonState.Normal;
DataGridView.InvalidateCell (this);
}
}
- protected override void Paint (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
+ protected override void Paint (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
- base.Paint (graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
+ base.Paint (graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}
internal override void PaintPartContent (Graphics graphics, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, object formattedValue)
{
CheckBoxState state;
-
- if ((bool)formattedValue == false)
+ CheckState value = GetCurrentValue ();
+
+ if ((CheckState)value == CheckState.Unchecked)
state = (CheckBoxState)check_state;
- else if ((bool)formattedValue == true)
+ else if ((CheckState)value == CheckState.Checked)
state = (CheckBoxState)((int)check_state + 4);
- else
+ else if (threeState)
state = (CheckBoxState)((int)check_state + 8);
-
+ else
+ state = (CheckBoxState)check_state;
+
Point p = new Point (cellBounds.X + (Size.Width - 13) / 2, cellBounds.Y + (Size.Height - 13) / 2);
CheckBoxRenderer.DrawCheckBox (graphics, p, state);
}
+ private CheckState GetCurrentValue ()
+ {
+ CheckState cs = CheckState.Indeterminate;
+
+ object current_obj;
+
+ if (editingCellValueChanged)
+ current_obj = editingCellFormattedValue;
+ else
+ current_obj = Value;
+
+ if (current_obj == null)
+ cs = CheckState.Indeterminate;
+ else if (current_obj is bool)
+ {
+ if ((bool)current_obj == true)
+ cs = CheckState.Checked;
+ else if ((bool)current_obj == false)
+ cs = CheckState.Unchecked;
+ }
+ else if (current_obj is CheckState)
+ cs = (CheckState)current_obj;
+
+ return cs;
+ }
+
protected class DataGridViewCheckBoxCellAccessibleObject : DataGridViewCellAccessibleObject {
public DataGridViewCheckBoxCellAccessibleObject (DataGridViewCell owner) : base(owner)
}
-#endif