Set Default date to today when adding a new row using EditType.DateTimePickerEdit

I have a EditType.DateTimePickerEdit column in my grid.  When I add a new row to the grid I want the default date to be today.

I've tried this in the Grid's ActionComplete event, but doesn't work:

    public async Task OnComplete(ActionEventArgs<Data.db.Announcements> Args)
    {
        f (Args.RequestType.ToString() == "Add")
        {
            Args.Data.DateExpire = DateTime.Now;
            
        }
    }

3 Replies 1 reply marked as answer

RN Rahul Narayanasamy Syncfusion Team October 21, 2020 11:09 AM UTC

Hi Michael, 
 
Greetings from Syncfusion. 
 
Query: Set Default date to today when adding a new row using EditType.DateTimePickerEdit 
 
We have validated your query and you want to set default date value to date column while adding the record. You can achieve your requirement by using DefaultValue property of GridColumn. Find the below code snippets and documentation for your reference. 
 
 
<SfGrid DataSource="@Orders" Height="315" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })"> 
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="120" DefaultValue="@("ANTON")"></GridColumn> 
        <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" DefaultValue="@(DateTime.Now)" EditType="EditType.DateTimePickerEdit" Format="d" TextAlign="TextAlign.Right" Width="130" Type="ColumnType.Date"></GridColumn> 
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
    </GridColumns> 
</SfGrid> 
 
Reference: 
 
Please let us know if you have any concerns. 
 
Regards, 
Rahul 


Marked as answer

ML Michael Lambert October 23, 2020 07:57 PM UTC

Thanks work for that instance, Thanks.

What happens if I have something like this, how would I set the DateStart to DateTime.Now for new records only?

        <GridColumn Field=@nameof(TimeSheets.DateStart) HeaderText="Start" Width="120" CustomFormat="@(new { type ="date", format="MM/dd/yyyy" })"                TextAlign="TextAlign.Right" Type="ColumnType.Date" ValidationRules="@(new ValidationRules{ Required = true })">
                      <EditTemplate>                             
                              <SfDateTimePicker @bind-Value="@((context as TimeSheets).DateStart)" Max="DateTime.Now" Min="@StartDate" ShowClearButton="true"                                    StrictMode="true">
                              </SfDateTimePicker>
                      </EditTemplate>
                      <Template>
                          @{
                              var t = (context as TimeSheets);
                              <span>@t.DateStart.ToString("MM/dd/yyyy HH:mm")</span>
                               }
                      </Template>
        </GridColumn>


MB Maran Baskar Syncfusion Team October 26, 2020 01:25 PM UTC

Hi Michael, 
 
Thanks for contacting Syncfusion Support. 
 
Query: What happens if I have something like this, how would I set the DateStart to DateTime.Now for new records only? 
 
From your query, we understand that you need to set default value as Datetime.Now when using EditTemplate while performing add action.  
You could achieve this with the DefaultValue property in Grid. The Default value given in GridColumn is  only set while performing “Add” action. 
Please refer the code snippet to achieve your requirement. 

        <GridColumn Field=@nameof(Order.OrderDate) DefaultValue=”DateTime.Now HeaderText=”Order Date” EditType=”EditType.DatePickerEdit Format=”d” Type=”ColumnType.Date Width=”160”> 
            <EditTemplate> 
                <SfDateTimePicker @bind-Value=”@((context as Order).OrderDate) Max=”DateTime.Now ShowClearButton=”true” StrictMode=”true”> 
                </SfDateTimePicker> 
            </EditTemplate> 
            <Template> 
                @{ 
                    var t = (context as Order); 
                    <span>@t.OrderDate</span> 
                } 
            </Template> 
        </GridColumn> 
 
 
Please get back to us, if you need further assistance. 
 
 
Regards, 
Maran Baskar 
 
 


Loader.
Up arrow icon