views:

104

answers:

1

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; }
    ...
}
+2  A: 

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".

Hans Passant
Yes, I know about this method. The reason is that I bind a data set automatically by setting grid's data source. Editing columns implies that I need to populate grid's columns manually.
Alex B