This example contains a similar approach to the
DateTimeField mentioned above. It is actually simpler to implement because it
does not have many challenges. For instance, here is the definition of the
previous four methods as shown above.
Listing 12
public override void BindEditControl(object control, bool insertMode)
{
CheckBox box = control as CheckBox;
if (!insertMode)
box.Checked = this.GetDataItemValue < bool > (box.NamingContainer,
this.DataField);
}
protected override string GetDataItemFieldName()
{
return this.DataField;
}
public override string GetEditControlValue(TableCell cell)
{
return this.ExtractControl < CheckBox > (cell).Checked.ToString();
}
public override Control SetupEditControl()
{
return new CheckBox();
}
Because the control is a checkbox, which the interface is not
manipulated by internal properties, it is easy to handle the interactions.
However, the CheckBoxField implements some of the additional functionality
available. For instance, this custom class defines a TrueString and FalseString
property values which convert the read-only value to something more
appropriate.
To do this, it requires catching the value being bound to
the table cell, and outputting a different value. I created a GetReadOnlyValue
method that changes the initial value into something else, all at your control.
The implementation that works with this custom field is shown below (note that
the default to TrueString and FalseString are the bool.TrueString and
bool.FalseString properties).
Listing 13
protected override string GetReadOnlyValue(object initialValue)
{
if (initialValue == null)
return bool.FalseString;
bool value = (bool)initialValue;
return value ? this.TrueString : this.FalseString;
}