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

GridDateTimeColumn and DateTime.MinValue

8 Answers 549 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pether Wiklander
Top achievements
Rank 2
Pether Wiklander asked on 27 Oct 2008, 02:03 PM
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

8 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 27 Oct 2008, 02:29 PM
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.
0
Pether Wiklander
Top achievements
Rank 2
answered on 27 Oct 2008, 02:39 PM
Hi Daniel,

That works great! Thank you!
// Pether
0
Pether Wiklander
Top achievements
Rank 2
answered on 27 Oct 2008, 04:42 PM
Is it possible to change the input field, when changing to edit mode, so that it is empty when SelectedDate is DateTime.MinValue?
0
Daniel
Telerik team
answered on 27 Oct 2008, 05:16 PM
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.
0
Pether Wiklander
Top achievements
Rank 2
answered on 28 Oct 2008, 08:22 AM
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

0
Accepted
Daniel
Telerik team
answered on 28 Oct 2008, 05:48 PM
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.
0
JJ
Top achievements
Rank 1
answered on 16 Nov 2011, 07:48 PM
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
0
Shinu
Top achievements
Rank 2
answered on 17 Nov 2011, 05:48 AM
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.
Tags
Grid
Asked by
Pether Wiklander
Top achievements
Rank 2
Answers by
Daniel
Telerik team
Pether Wiklander
Top achievements
Rank 2
JJ
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or