GridDateTimeColumn and DateTime.MinValue

9 posts, 2 answers
  1. Pether Wiklander
    Pether Wiklander avatar
    17 posts
    Member since:
    Oct 2008

    Posted 27 Oct 2008 Link to this post

    Hi,

    My data source returns DateTime.MinValue as null when no date is set by the user. This will be shown as 0001-01-01 00:00:00 in the RadGrid.

    Question 1: Is it possible to change the behavior so that DateTime.MinValue will result in EmptyDataText showing?

    Question 2: When changing into edit mode... How can I avoid getting exceptions when SelectedDate is DateTime.MinValue?

    Best regards,
    Pether Wiklander
  2. Answer
    Daniel
    Admin
    Daniel avatar
    4946 posts

    Posted 27 Oct 2008 Link to this post

    Hello Pether,

    Test whether the following approach is suitable for your scenario:

    Q1:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        if (e.Item is GridDataItem && DateTime.Parse(((GridDataItem)e.Item)["myDateColumn"].Text) == DateTime.MinValue) 
            ((GridDataItem)e.Item)["myDateColumn"].Text = "no date"




    Q2:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
        if (e.Item.IsInEditMode) 
            ((e.Item as GridEditFormItem)["myDateColumn"].Controls[0] as RadDatePicker).MinDate = DateTime.MinValue; 

    Kind regards,
    Daniel
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Pether Wiklander
    Pether Wiklander avatar
    17 posts
    Member since:
    Oct 2008

    Posted 27 Oct 2008 Link to this post

    Hi Daniel,

    That works great! Thank you!
    // Pether
  5. Pether Wiklander
    Pether Wiklander avatar
    17 posts
    Member since:
    Oct 2008

    Posted 27 Oct 2008 Link to this post

    Is it possible to change the input field, when changing to edit mode, so that it is empty when SelectedDate is DateTime.MinValue?
  6. Daniel
    Admin
    Daniel avatar
    4946 posts

    Posted 27 Oct 2008 Link to this post

    Hello Pether,

    Please try this code-snippet:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
        if (e.Item.IsInEditMode) 
        { 
            RadDatePicker datePicker = (e.Item as GridEditFormItem)["myDateColumn"].Controls[0] as RadDatePicker; 
            if (datePicker.SelectedDate == DateTime.MinValue) 
                datePicker.DateInput.Text = ""
        } 

    Kind regards,
    Daniel
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  7. Pether Wiklander
    Pether Wiklander avatar
    17 posts
    Member since:
    Oct 2008

    Posted 28 Oct 2008 Link to this post

    Hi Daniel,

    The textbox still shows 0001-01-01. When I debug, I can see that SelectedValue is still null when the item is created. I tried changing the condition to

    if( datePicker.SelectedDate == DateTime.MinValue || datePicker.SelectedDate == null )
    datePicker.DateInput.Text = string.Empty; ...but no luck. Any suggestions?

    // Pether

  8. Answer
    Daniel
    Admin
    Daniel avatar
    4946 posts

    Posted 28 Oct 2008 Link to this post

    Hello Pether,

    I suggest you to include a little modification to the previous code:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item.IsInEditMode)  
        {  
            RadDatePicker datePicker = (e.Item as GridEditFormItem)["myDateColumn"].Controls[0] as RadDatePicker;  
            if (datePicker.SelectedDate == DateTime.MinValue)  
                datePicker.Clear();  
        }  
    }  

    Best regards,
    Daniel
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  9. JJ
    JJ avatar
    147 posts
    Member since:
    May 2010

    Posted 16 Nov 2011 Link to this post

    I am still not able to make the datetime be empty when in edit mode while it is MinDate.

    On ItemCreated, I tried below, not work

    RadDatePicker datePicker = (e.Item as GridEditFormItem)["ExpirationDate"].Controls[0] as RadDatePicker;
      
    ((e.Item as GridEditFormItem)["ExpirationDate"].Controls[0] as RadDatePicker).MinDate = DateTime.MinValue;
      
    if (datePicker.SelectedDate == DateTime.MinValue || datePicker.SelectedDate == null)
    {
    datePicker.DbSelectedDate = null;
    datePicker.Clear(); 
    }

    Please help if you know.

    Thanks
  10. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 16 Nov 2011 Link to this post

    Hello,,

    Try the following code snippet in ItemDataBound event which worked as expected.
    C#:
    protected void grid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
     if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
      {
        GridEditFormItemitm = (GridEditFormItem)e.Item;
        RadDatePicker pkr = (RadDatePicker)itm["ExpirationDate"].Controls[0];
        if (pkr.SelectedDate == DateTime.MinValue)
        pkr.Clear();
      }
    }

    -Shinu.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017