We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to set default value of nullable GridDateTimeColumn to current date

Thread ID:

Created:

Updated:

Platform:

Replies:

151381 Feb 10,2020 06:42 AM UTC Feb 11,2020 09:19 AM UTC WinForms 3
loading
Tags: SfDataGrid
Vasyl Shepelyov
Asked On February 10, 2020 06:42 AM UTC

Hi,
I have sfDataGrid with few nullable GridDateTimeColumn columns. When there is a null in a cell and user begins editing a cell, cell gets populated with default value - MinDateTime date, but I'd like default date to be current date instead. How can I achieve it?

I've figured out how to handle begin edit event and how to check that current cell's value is null, but I don't know how to set cell's value to current date then.

Using .SetControlValue doesn't help. Maybe I need to move cell to edit state somehow first? Or perhaps there is some other alternative.




Thank you.


Susmitha Sundar [Syncfusion]
Replied On February 10, 2020 02:18 PM UTC

Hi Vasyl Shepelyov, 
 
Thank you for using Syncfusion controls. 
 
By default, CurrentCellBeginEdit event initialize the editing and we did not get the editor from this event. So we can’t set the value using SetControlValue() method. But you can achieve your requirement in two ways, 
 
Way 1:  
 
You can set the MinimumDate as today date in GridDateTimeColumn. Please refer the below code, 
 
C#: 
   this.sfDataGrid1.Columns.Add(new GridDateTimeColumn() { MappingName = "DateTime", AllowNull = true,  Format = "MM/dd/yyyy", MinDateTime=DateTime.Now }); 
 
Way 2: 
 
You can set the value for RowData in CurrentCellBeginEdit event. Please refer the below code, 
 
C#: 
private void SfDataGrid1_CurrentCellBeginEdit(object sender, Syncfusion.WinForms.DataGrid.Events.CurrentCellBeginEditEventArgs e) 
{ 
    if (this.sfDataGrid1.CurrentCell.CellRenderer.GetControlValue() == null) 
    { 
        var rowData = e.DataRow.RowData as OrderInfo; 
        rowData.DateTime = DateTime.Now; 
    } 
} 
 
 
Please check the sample and let us know if you need further assistance on this. 
 
Regards, 
Susmitha S 


Vasyl Shepelyov
Replied On February 10, 2020 02:30 PM UTC

Hi Susmitha,

Second approach works perfectly fine for my scenario. First approach could also work, but I wanted user to be able to select date in the past if he wants, that's why I was looking for the way to handle cell change event rather than restricting MinDateTime to current date.

Many thanks for your help.

Best regards,
Vasyl

Farjana Parveen Ayubb [Syncfusion]
Replied On February 11, 2020 09:19 AM UTC

Hi Vasyl, 
 
Thanks for the update. 
 
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you. 
 
Regards, 
Farjana Parveen A 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon