Set default date for grid column when adding new row in grid

Hi There,
I've a grid and in one of the column, I'm using edit template which has datepicker inside it. When I add a new record I want to populate a default datetime (which is datetime.now + 14 days) for this column and I want it editable, so if user wants to change it then they can select any other date in datepicker. How can I do this? I've tried putting default value in my model for that date property but that doesn't work. It comes without any value.

1 Reply

VN Vignesh Natarajan Syncfusion Team May 12, 2020 04:00 AM UTC

Hi Preity,  
 
Thanks for contacting Syncfusion support.  
 
Query: “When I add a new record I want to populate a default datetime (which is datetime.now + 14 days) for this column and I want it editable 
 
We suggest you to achieve your requirement using DefaultValue property of the GridColumn. Refer the below code example 
 
<SfGrid DataSource="@Orders" AllowPaging="true" Toolbar="@(new List<string>() { "Add""Edit""Delete""Cancel""Update" })" Height="315"> 
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings> 
    <GridColumns> 
        . . . . . .. .  
        <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" EditType="EditType.DatePickerEdit" DefaultValue="@DefaultDate" Format="dd/MM/yyyy" TextAlign="TextAlign.Right" Width="130" Type="ColumnType.Date"> 
            <EditTemplate> 
                <SfDatePicker ID="OrderDate" Value="@((context as Order).OrderDate)" Enabled="@allowed" Format="dd/MM/yyyy" TValue="DateTime?"></SfDatePicker> 
            </EditTemplate> 
        </GridColumn> 
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
    </GridColumns> 
</SfGrid> 
  
@code{ 
    public bool allowed = false; 
    public DateTime? DefaultDate = DateTime.Now.AddDays(+14); 
    . .. . . 
} 
 
 
For your convenience we have attached the sample which can be downloaded from below  
 
 
Note: DatePicker will be disabled state, it will be enabled once you select a value from CustomerID column dropdownlist. If you want to enable DatePicker on initial rendering. Kindly remove the Enabled property from SfDatePicker. We have provided this solution based on sample provided in F152607  
 
Refer our UG documentation for your reference 
 
 
Kindly get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan 


Loader.
Up arrow icon