iformatprovider

Using a custom formatter in a DataGridView

人走茶凉 提交于 2019-11-28 11:37:17
So, maybe this is a bad design; I don't know. But say I have a DataTable with a column that holds int values; these values are in fact meant to represent some enum type that I have in my project. What I'd like to do is have a DataGridView bound to this table and have the column display the name of the enum rather than the integer value "0" or "1" or whatever. One option I considered was to do the whole normalization thing: add a table in the DataSet with the enum names in it, keyed on the enum values, and have my first table hold a reference to this table. But this is an enum -specific idea. I

How to create and use a custom IFormatProvider for DateTime?

試著忘記壹切 提交于 2019-11-27 21:59:56
I was trying to create an IFormatProvider implementation that would recognize custom format strings for DateTime objects. Here is my implementation: public class MyDateFormatProvider : IFormatProvider, ICustomFormatter { public object GetFormat(Type formatType) { if (formatType == typeof(ICustomFormatter)) { return this; } return null; } public string Format(string format, object arg, IFormatProvider formatProvider) { if(arg == null) throw new ArgumentNullException("arg"); if (arg.GetType() != typeof(DateTime)) return arg.ToString(); DateTime date = (DateTime)arg; switch(format) { case

How to create and use a custom IFormatProvider for DateTime?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 20:35:47
问题 I was trying to create an IFormatProvider implementation that would recognize custom format strings for DateTime objects. Here is my implementation: public class MyDateFormatProvider : IFormatProvider, ICustomFormatter { public object GetFormat(Type formatType) { if (formatType == typeof(ICustomFormatter)) { return this; } return null; } public string Format(string format, object arg, IFormatProvider formatProvider) { if(arg == null) throw new ArgumentNullException("arg"); if (arg.GetType() !