Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8933661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:35:52+00:00 2026-06-15T09:35:52+00:00

I’m converting some application code to use NodaTime classes instead of System.DateTime. Part of

  • 0

I’m converting some application code to use NodaTime classes instead of System.DateTime. Part of my application uses the PropertyGrid control to allow a user to edit a class containing both a LocalDate and an Instant. Without changing anything, the PropertyGrid displays the properties okay, but they are no longer editable. What’s the best way of allowing the user to edit these fields.

For the sake of exposition, we can use this class as a representative of the type of thing I’d like to display and edit:

public class User
{
    public string Name { get; set; }
    public LocalDate BirthDate { get; set; }
    public Instant NextAppointment { get; set; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:35:53+00:00Added an answer on June 15, 2026 at 9:35 am

    Best I’ve come up with so far:

    Step 1: Create TypeConverter’s so that the Noda classes are editable

    public class ToAndFromStringTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
                return true;
            else
                return base.CanConvertFrom(context, sourceType);
        }
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
                return true;
            else
                return base.CanConvertTo(context, destinationType);
        }
    }
    
    public class LocalDateTypeConverter : ToAndFromStringTypeConverter
    {
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                DateTime parsed;
                if (!DateTime.TryParse((string)value, out parsed))
                    throw new ArgumentException("Cannot convert '" + (string)value + "' to LocalDate.");
                else
                    return new LocalDate(parsed.Year, parsed.Month, parsed.Day);
            }
            else
            {
                return base.ConvertFrom(context, culture, value);
            }
        }
    
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                var tvalue = (LocalDate)value;                
                try
                {
                    var x = tvalue.ToString("yyyy-MM-dd");
                    return x;
                }
                catch (NullReferenceException)
                {
                    return "1900-1-1";
                }
                catch
                {
                    throw new ArgumentException("Could not convert '" + value.ToString() + "' to LocalDate.");
                }                
            } 
            else 
                return base.ConvertTo(context, culture, value, destinationType);
        }
    
    public class InstantTypeConverter : ToAndFromStringTypeConverter
    {
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                try
                {
                    DateTime parsed = DateTime.Parse((string)value);
                    LocalDateTime dt = LocalDateTime.FromDateTime(parsed);
                    Instant i = dt.InZoneLeniently(DateTimeZoneProviders.Default.GetSystemDefault()).ToInstant();
                    return i;
                }
                catch
                {
                    throw new ArgumentException("Cannot convert '" + (string)value + "' to Instant.");
                }
            }
            else
            {
                return base.ConvertFrom(context, culture, value);
            }
        }
    
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                try
                {
                    Instant tvalue = (Instant)value;
                    LocalDateTime local = tvalue.InZone(DateTimeZoneProviders.Default.GetSystemDefault()).LocalDateTime;
                    string output = LocalDateTimePattern.CreateWithInvariantCulture("yyyy-MM-dd HH:mm:ss.FFFFFF").Format(local);
                    return output;
                }
                catch
                {
                    throw new ArgumentException("Could not convert '" + value.ToString() + "' to LocalDate.");
                }                    
            }
            else
                return base.ConvertTo(context, culture, value, destinationType);
        }
    }
    

    Step 2: Register TypeConverters

    Put this code at the top of your app:

    TypeDescriptor.AddAttributes(typeof(LocalDate), new TypeConverterAttribute(typeof(LocalDateTypeConverter)));
    TypeDescriptor.AddAttributes(typeof(Instant), new TypeConverterAttribute(typeof(InstantTypeConverter)));
    

    Step 3: Use custom collection editor to handle things like List

    public class NodaCollectionEditor : System.ComponentModel.Design.CollectionEditor
    {
        public NodaCollectionEditor(Type collection_type) : base(collection_type) { }
    
        protected override object CreateInstance(Type itemType)
        {
            if (itemType == typeof(LocalDate))
                return LocalDateHelper.MinValue;
            else 
                return base.CreateInstance(itemType);
        }
    }
    

    This can be registered by adding this attribute to any appropriate properties:

    [System.ComponentModel.Editor(typeof(NodaCollectionEditor),typeof(System.Drawing.Design.UITypeEditor))]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.