开发者

GetFormat never gets an IFormatProvider type

开发者 https://www.devze.com 2023-02-16 08:01 出处:网络
In a WinGrid (Infragistics, if you must know) I got a column containing ints. The value is a number of seconds, from which you can calculate a time. I created an IFormatProvider/ICustomFormatter that

In a WinGrid (Infragistics, if you must know) I got a column containing ints. The value is a number of seconds, from which you can calculate a time. I created an IFormatProvider/ICustomFormatter that does just that. During my grid initialization, I set the Format and FormatInfo parameters.

However, when GetFormat is called on my custom type formatter, the type parameter is always a NumberFormatInfo, and never an ICustomFormatter. Why?

Here is my class, in case it helps :

public class SecToTime : IFormatProvider, ICustomFormatter
{
    public object GetFormat(Type formatType)
    {
        if (formatType == typeof(ICustomFormatter))
        {
            return this;
        }
        else
        {
            return null;
        }
    }

    public string Format(string format, object arg, IFormatProvider provider)
    {
        if (arg is int)
        {
            int seconds 开发者_如何学JAVA= (int)arg;
            int hours = (int)Math.Truncate((double)seconds / 3600);
            int minutes = (int)Math.Truncate((double)(seconds / 60) % 60);
            seconds = seconds % 60;
            return string.Format("{0:hh:mm:ss}", new DateTime(0, 0, 0, hours, minutes, seconds));
        }
        else
            throw new ArgumentNullException();
    }
}


Quoting the (great) Mike Salzman from Infragistics Team:

The FormatInfo will not be called unless the Format property is also set. That's the only reason I can think of why it would not be called.

Source: this Infragistics forum's post

To test it, try to set the column Format property to something... :)

0

精彩评论

暂无评论...
验证码 换一张
取 消