When the control interface is created, an event handler is
setup to respond to the data binding. At this point, the control has not been
bound and needs to be assigned a value. As you can see below, this method
handles getting.
Listing 6
void control_DataBinding(object sender, EventArgs e)
{
if (sender is TableCell)
{
TableCell cell = sender as TableCell;
cell.Text = this.GetDataItemValue<string>(cell.NamingContainer,
this.DataField));
}
else
{
CheckBox box = control as CheckBox;
if (!insertMode)
box.Checked = this.GetDataItemValue<bool>(box.NamingContainer,
this.DataField);
}
}
The GetDataItemValue is a method I illustrated above; it
uses the naming container for the actual control, and performs the action to
get the reference to the actual value, passing it into the Checked
property. This assignment here is to the Checked property, but could also
be an assignment to the Text property of a TextBox, the SelectedDate property
of a Calendar, the SelectedValue property of a DropDownList, and many more.
However, in readonly mode, the cell renders a readonly value, which uses the
following method to render a more appropriate method.
Listing 7
protected virtual string GetReadOnlyValue(object initialValue)
{
if (initialValue != null)
return initialValue.ToString();
else
return null;
}
This does not seem very helpful, but it can be very helpful
in allowing a custom data field to render a formatted read-only value that is
appropriate. For instance, for a boolean field I created, I used it to
change the text from True/False database values to the values that are stored
in two property values within the control (so by setting the TrueString and
FalseString properties of the custom data field, it translates the database
boolean value into a more readable form in read-only mode). In addition,
for a numerical field I created, I can use this to prefix the text with $
and suffix it with .00, making it more meaningful to show whole dollar amounts.