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

Picking Today's Date on RadGrid Inline Insert

2 Answers 356 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Elmer Boutin
Top achievements
Rank 2
Elmer Boutin asked on 27 Oct 2009, 02:48 PM
I'm using RadControls for ASP.NET AJAX Q2 2009. I have a RadGrid set up with a GridDateTimeColumn with the PickerType="DatePicker" and EditMode="InPlace".

    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" GridLines="None" 
        Width="100%" Height="175px" DataSourceID="odsDataSource" OnItemCreated="RadGrid1_ItemCreated" 
        ShowStatusBar="True" OnPreRender="RadGrid1_PreRender" OnItemInserted="RadGrid1_ItemInserted" 
        OnItemUpdated="RadGrid1_ItemUpdated" OnItemCommand="RadGrid1_ItemCommand" 
        OnInsertCommand="RadGrid1_InsertCommand"  
        OnUpdateCommand="RadGrid1_UpdateCommand" OnDeleteCommand="RadGrid1_DeleteCommand" 
        ClientSettings-Scrolling-AllowScroll="true" ClientSettings-Scrolling-SaveScrollPosition="true" 
        AllowAutomaticInserts="False" AllowAutomaticUpdates="False" AllowAutomaticDeletes="false" 
        Visible="False" HeaderStyle-Font-Size="XX-Small" HeaderStyle-Font-Bold="true" 
        Font-Size="XX-Small" TabIndex="50"
        <MasterTableView CommandItemDisplay="Top" CommandItemSettings-RefreshText="Refresh List" DataSourceID="odsDataSource" 
            ShowHeadersWhenNoRecords="true" NoMasterRecordsText="No Entries" EditMode="InPlace" 
            ItemStyle-Font-Size="XX-Small" Font-Size="XX-Small" EditItemStyle-Font-Size="XX-Small" 
            EditFormSettings-EditColumn-ItemStyle-Font-Size="XX-Small"
            <CommandItemSettings RefreshText="Refresh List"></CommandItemSettings> 
            <Columns> 
                            <telerik:GridEditCommandColumn ButtonType="LinkButton" EditText="Edit" CancelText="Cancel" 
                    UniqueName="EditColumn" ItemStyle-Font-Size="XX-Small"
                </telerik:GridEditCommandColumn> 
                 
                <telerik:GridDateTimeColumn DataField="CHECKER_DATE" HeaderText="Date" ReadOnly="false" 
                    UniqueName="CHECKER_DATE" ItemStyle-Font-Size="XX-Small" ColumnEditorID="CHECKER_DATE" 
                    Resizable="false" PickerType="DatePicker" DataFormatString="{0:MM/dd/yyyy}" MinDate="2007-10-01"
                </telerik:GridDateTimeColumn> 
            </Columns>             
<!-- Big grid. Other columns truncated to save space -->
        </MasterTableView> 
</telerik:RadGrid> 

When I click on a row's "Edit' link, the DatePicker shows the correct date from the database in both the box and in the calendar. However, when I click on "Insert" I want the current date to be selected and I can't seem to get that to happen.

In the codebehind page I have this method set up:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
     if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
     { 
           GridEditableItem item = e.Item as GridEditableItem; 
           if (e.Item.ItemIndex == -1) 
           {   
                (editedItem["CHECKER_DATE"].Controls[0] as RadDatePicker).SelectedDate = DateTime.Today
           } 
     } 

I have also tried it like this:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
{  
     if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
     {  
           GridEditableItem item = e.Item as GridEditableItem;  
           if ((editedItem["CHECKER_DATE"].Controls[0] as RadDatePicker).SelectedDate.ToString().Length == 0) 
            {                
             editedItem["CHECKER_DATE"].Controls[0] as RadDatePicker).SelectedDate = DateTime.Today; 
            }  
     }  
}  

I have also tried editedItem["CHECKER_DATE"].Controls[0] as RadDatePicker).SelectedDate = DateTime.Now.Date; with no success.

I've tried some other methods which I've found on Telerik's web site and some other places and I can't seem to get this date to pre-select. 

Any ideas of what I can do to make this work.

Thanks in advance.

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 28 Oct 2009, 06:45 AM
Hi Elmer,

Try the following code snippet in order to set current date to DatePicker when in Insert mode.

cs:
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    if (e.Item is GridDataInsertItem && e.Item.OwnerTableView.IsItemInserted)  
    { 
        GridDataInsertItem insertItem = (GridDataInsertItem)e.Item; 
        (insertItem["CHECKER_DATE"].Controls[0] as RadDatePicker).SelectedDate = DateTime.Today; 
    } 

-Shinu.
0
Elmer Boutin
Top achievements
Rank 2
answered on 28 Oct 2009, 02:39 PM
Shinu,

Thank you for that solution. It worked perfectly.

I did not think to put that code into the ItemDataBound method because this was on an insert command.
Tags
Grid
Asked by
Elmer Boutin
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Elmer Boutin
Top achievements
Rank 2
Share this question
or