tags:

views:

12

answers:

1

Here's the documentation. I haven't found any explanation anywhere. There is a data binding overview but is for WPF, and I'm using WinForms. I thought what it does is to call whatever method I assign to the event Format of the Binding class, but it will call it even if I set formattingEnabled to false as long as I assign a method. So now I don't know what it does, and I don't understand where is people supposed to get this kind of information.

+1  A: 

It looks like you need a couple pieces... First here's the Reflector'd bit on the Format event that you've added

protected virtual void OnFormat(ConvertEventArgs cevent)
{
    if (this.onFormat != null)
    {
        this.onFormat(this, cevent);
    }
    if (((!this.formattingEnabled && !(cevent.Value is DBNull)) && ((cevent.DesiredType != null) && !cevent.DesiredType.IsInstanceOfType(cevent.Value))) && (cevent.Value is IConvertible))
    {
        cevent.Value = Convert.ChangeType(cevent.Value, cevent.DesiredType, CultureInfo.CurrentCulture);
    }
}

and then there's this:

private object FormatObject(object value)
{
    if (this.ControlAtDesignTime())
    {
        return value;
    }
    Type propertyType = this.propInfo.PropertyType;
    if (this.formattingEnabled)
    {
        ConvertEventArgs args = new ConvertEventArgs(value, propertyType);
        this.OnFormat(args);
        if (args.Value != value)
        {
            return args.Value;
        }
        TypeConverter sourceConverter = null;
        if (this.bindToObject.FieldInfo != null)
        {
            sourceConverter = this.bindToObject.FieldInfo.Converter;
        }
        return Formatter.FormatObject(value, propertyType, sourceConverter, this.propInfoConverter, this.formatString, this.formatInfo, this.nullValue, this.dsNullValue);
    }
    ConvertEventArgs cevent = new ConvertEventArgs(value, propertyType);
    this.OnFormat(cevent);
    object obj2 = cevent.Value;
    if (propertyType == typeof(object))
    {
        return value;
    }
    if ((obj2 != null) && (obj2.GetType().IsSubclassOf(propertyType) || (obj2.GetType() == propertyType)))
    {
        return obj2;
    }
    TypeConverter converter2 = TypeDescriptor.GetConverter((value != null) ? value.GetType() : typeof(object));
    if ((converter2 != null) && converter2.CanConvertTo(propertyType))
    {
        return converter2.ConvertTo(value, propertyType);
    }
    if (value is IConvertible)
    {
        obj2 = Convert.ChangeType(value, propertyType, CultureInfo.CurrentCulture);
        if ((obj2 != null) && (obj2.GetType().IsSubclassOf(propertyType) || (obj2.GetType() == propertyType)))
        {
            return obj2;
        }
    }
    throw new FormatException(SR.GetString("ListBindingFormatFailed"));
}

So it's still going to format the object according to what you've bound to the Format event handler.

drachenstern
:S Did you answer a different question by accident?
jsoldi
@jsoldi ~ You don't use RedGate Reflector much do you?
drachenstern
Not at all.....
jsoldi
These are the actual internal methods used by the Binding object, you should probably pick up a free copy of RedGate Reflector and learn how to use it, because it's incredibly helpful when you're frustrated by stuff like this. [http://www.red-gate.com/products/reflector/](http://www.red-gate.com/products/reflector/)
drachenstern