This is a migrated thread and some comments may be shown as answers.

[Solved] Allowing blank date in Edit mode

1 Answer 110 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
John
Top achievements
Rank 1
John asked on 02 Feb 2011, 10:59 PM
Greetings,
Due to the nature of work, we have a RadGridview which holds on datetime column.  When a value of that column is Minvalue (1/1/0001) then we show blank.  

In the Edit Mode, it is allowed to have a blank date value but we got the error.

We are using resource to control it as follow:
public class NullableDateFormatter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var dt = (Nullable<DateTime>)value;
 
        if (dt != null && dt.HasValue && !dt.Equals(new DateTime()))
            return dt;
 
        return string.Empty;
    }
 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //string strValue = value as string;
        //DateTime resultDateTime;
        //if (DateTime.TryParse(strValue, out resultDateTime))
        //    return resultDateTime;
        //return DependencyProperty.UnsetValue;
        return null;
    }
 
}


in our .xaml file, we have a column defined as:
....
RowEditEnded="dgPhasesEdit_RowEditEnded"
CellValidating="dgPhasesEdit_CellValidating"
......

<telerik:GridViewDataColumn Header="Acceptance Needed" DataMemberBinding="{Binding Acceptance_Needed_Date, StringFormat='M/dd/yyyy', Converter={StaticResource nullableDateFormatter}}"  Width="*">
    <telerik:GridViewDataColumn.CellStyle>
        <Style TargetType="telerik:GridViewCell">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
    </telerik:GridViewDataColumn.CellStyle>
</telerik:GridViewDataColumn>

In our .xaml.cs file, we have this function to allow the blank Datetime value:
private void dgPhasesEdit_CellValidating(object sender, GridViewCellValidatingEventArgs e)
{
    if (e.Cell.Column.UniqueName.Equals("Acceptance_Needed_Date"))
    {
        object newValue = e.NewValue;
        if (newValue == null)   //Allow the null/blank Date value
            return;
        if (!Helper.IsDateTime(newValue))
        {
            e.IsValid = false;
            e.ErrorMessage = "Invalid DateTime format";
        }
    }
}


However, when we run the app, and blank is given at that Datetime value, the error shows as followed:

http://i12.photobucket.com/albums/a201/le9569/1-6.jpg

Am I missing something?  I have noticed that when a blank value is called, it calls the Convertback function in the NullableDateFormatter  class.

Help is appreciated.
John.

1 Answer, 1 is accepted

Sort by
0
Accepted
Maya
Telerik team
answered on 03 Feb 2011, 10:23 AM
Hello John,

May you try to define the property in that particular column to be nullable. For example:

private DateTime? established;
public DateTime? Established
 {
       get { return this.established; }
       set
       {
          if (value != this.established)
          {
               this.established = value;
               this.OnPropertyChanged("Established");
           }
        }
  }

 

All the best,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
GridView
Asked by
John
Top achievements
Rank 1
Answers by
Maya
Telerik team
Share this question
or