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

Initializing a GridDateTimeColumn When Inserting a New Item

2 Answers 73 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Leonard Jones
Top achievements
Rank 1
Leonard Jones asked on 11 Oct 2010, 12:35 AM
I wish to set the value of a GridDateTimeColumn to today's date when inserting a new row into a grid - see the following code extract. The code does not error but when the edit row is displayed the textbox in the date column is blank. Any help would be great.

Rgds

Len Jones


protected void grid_ItemCreated(object sender, GridItemEventArgs e)
  
{
  
    if (e.Item.OwnerTableView.Name == "gridTableView" && e.Item is GridEditableItem &&     e.Item.IsInEditMode)
  
{
  
    GridEditableItem editItem = e.Item as GridEditableItem;
  
    RadDatePicker picker = editItem["StartDate"].Controls[0] as RadDatePicker;
  
    if (e.Item.OwnerTableView.IsItemInserted)
  
    {
  
        picker.SelectedDate = DateTime.Today;
  
    }
  
}

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 11 Oct 2010, 05:56 AM
Hello Leonard,

ItemCreated is fired before the item is data-bound. Thus no data is still in the cells' text or input controls. So try the above code in ItemDataBound event rather than ItemCreated event.

And please go through the following documentation.
Distinguishing the major differences between ItemCreated and ItemDataBound events

Thanks,
Princy.
0
Leonard Jones
Top achievements
Rank 1
answered on 11 Oct 2010, 12:50 PM
Many thanks for your prompt reply. It's obvious when it's pointed out to you.

Rgds Len
Tags
Grid
Asked by
Leonard Jones
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Leonard Jones
Top achievements
Rank 1
Share this question
or