Problem with DateTime fields in property grid

0
0

Hi,

I’m trying to get a property grid to display a drop down calendar for a DateTime field but not having much luck.

When the form is loaded and the property grid displays, I click into the DateTime field to set the date value where I can see the drop down button/icon at the right of the property grid field, but when this is pressed, nothing is displayed.  If I then click off the field into another field in the property grid (i.e. lose the focus), an error popup appears saying that ‘Property value is not valid. Object of type System.DBNull cannot be converted to type System.DateTime’.

So I tried adding a DateTimePicker control and setting it as the custom editor in the propertyGrid1_SelectedGridItemBeginEdit event which did display the drop down calendar but that error popup appears as soon as the DateTime field gets the focus and enters an edit state.

Also, if I click into the DateTime field in the property grid and manually enter a date, this will work, but then if I select a different cell or control, then try to go back and edit the date again, the field doesn’t enter an edit state, no cursor displayed or drop down button, preventing me from editing the date again.

Any pointers or work-arounds on achieving this functionality?

  • You must to post comments
0
0

Hi Andrew,

WJ-8297 is included in Wisej build 1.3.82.

Best regards
Frank

  • You must to post comments
0
0

Thanks Luca & Frank.  It looks like the first option works ok.  It takes a few mouse clicks to get the date field in the property grid to enter an edit mode, but it works.

However, I’ve noticed a different problem.  If I add the following:

private void propertyGrid1_SelectedGridItemBeginEdit(object s, SelectedGridItemBeginEditEventArgs e)
{

if (e.GridItem.PropertyDescriptor.PropertyType == typeof(DateTime))
{

DateTimePicker dtPicker = new DateTimePicker();
dtPicker.Format = DateTimePickerFormat.Custom;
dtPicker.CustomFormat = “dd-MMM-yyyy”;
e.Editor = dtPicker;

}
}

…  when the DateTime field in the property grid gets the focus, the date format ‘dd-MMM-yyyy’ is used, which is correct, but as soon as that DateTime field loses the focus, the date format reverts to ‘M/dd/yyyy’ format.  This is a bit odd because if I add a DateTimePicker control directly onto the form then set the custom format to ‘dd-MMM-yyyy’, that format remains regardless if the control has the focus or not, so there must be some underlying setting that is controlling that display format in the property grid.

I checked the regional settings on my computer and short date is definitely set to ‘d/MM/yyyy’ so all good there. Maybe there needs to be 2 properties to control the date format, one for editing and one for displaying/viewing.

 

  • Luca (ITG)
    The formatting set to the editor is different from the format used when displaying the value. The PropertyGrid uses the System.ComponentModel architecture, including TypeConverter and UITypeEditor. A TypeConverter lets you control how a property is displayed. However, in the last release we also added the event PropertyValueFormatting which lets you control the formatting of a field without having to create a TypeConverter.
  • You must to post comments
0
0

Hi Andrew,

we have logged this issue as WJ-8297 and a fix will be included in the next Wisej build.

Best regards
Frank

  • You must to post comments
0
0

Yes, unfortunately we missed that it picks up the System.ComponentModel.Design.DateTimeEditor.

There are two possible workarounds:

1. Override or handle PropertyGrid.SelectedGridItemBeginEdit:

private void propertyGrid1_SelectedGridItemBeginEdit(object s, SelectedGridItemBeginEditEventArgs e)
{
if (e.GridItem.PropertyDescriptor.PropertyType == typeof(DateTime))
e.Editor = new DateTimePicker();
}

This lets you fully control any cell editor in the PropertyGrid.

2. Or modify the TypeDescriptor to return null to let the Wisej.Web.PropertyGrid use the default DateTimePicker:

This takes as bit more code.

You need two classes like this.

internal class DateTimeTypeDescriptorProvider : TypeDescriptionProvider
{
private static TypeDescriptionProvider defaultTypeProvider = TypeDescriptor.GetProvider(typeof(DateTime));

        public DateTimeTypeDescriptorProvider() : base(defaultTypeProvider){}

       public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{return new DateTimeTypeDescriptor(base.GetTypeDescriptor(objectType, instance), instance);}

}

internal class DateTimeTypeDescriptor : CustomTypeDescriptor
{
public DateTimeTypeDescriptor(ICustomTypeDescriptor parent, object instance) : base(parent) {}

      public override object GetEditor(Type editorBaseType) { return null;}
}

Then you can override the system’s TypeDescriptor for DateTime at startup or at any time before the PropertyGrid is created.

if ((TypeDescriptor.GetEditor(typeof(DateTime), typeof(UITypeEditor)) != null))
TypeDescriptor.AddProvider(new DateTimeTypeDescriptorProvider(), typeof(DateTime));

  • You must to post comments
Showing 4 results
Your Answer

Please first to submit.