开发者

How to pass a custom format string to DateTimeConverter?

I have a list of business logic objects that is bound to a Winforms DataGridView, and it contains DateTime fields. By default they get converted into a long date and time string.

I want the fields to be converted automatically into HH:MM format. I开发者_如何学运维've discovered that I can attach a TypeConverterAttribute to a field with a subclass of TypeConverter class, but I can't for the life of me figure out how to make DateTimeConverter accept a custom format string. Is it possible without writing a custom TypeConverter?

class Foo
{
    ...
    // How to make this converter use a custom format string?
    [TypeConverter(typeof(DateTimeConverter))] 
    public DateTime SomeDateField { get; private set; }
    ...
}


Not sure why you'd consider a TypeConverter. Select the DGV in your form and click the Tasks glyph on the upper right, Edit Columns. Select the column, then DefaultCellStyle on the upper right. Click the dots. Set Format to "HH:MM".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜