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. Image for the cookie policy date
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 Mar 19,2021 12:09 PM UTC WinForms 5
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 


Vladimir
Replied On March 18, 2021 07:35 PM UTC

This is not working in case of DataSet binding inside Grouping.
It would be possible to add something like DefaultValue for GridDateTimeColumn?
Thank You for an advice :)

Mohanram Anbukkarasu [Syncfusion]
Replied On March 19, 2021 12:09 PM UTC

Hi Vladimir, 

Thanks for the update.  

SfDataGrid doesn’t have any direct support to set default value for the GridDateTimeColumn.  However it is possible to assign a default value to the DateTime column of the underlying data when the date value is null or empty by creating a custom renderer for DateTime column as shown in the following code example.  

Code example

public Form1() 
{ 
    InitializeComponent(); 
 
    this.sfDataGrid1.CellRenderers["DateTime"] = new CustomDateTimeRenderer(this.sfDataGrid1); 
} 
 
public class CustomDateTimeRenderer : GridDateTimeCellRenderer 
{ 
    SfDataGrid DataGrid { get; set; } 
    public CustomDateTimeRenderer(SfDataGrid dataGrid) 
    { 
        this.DataGrid = dataGrid; 
    } 
    protected override void OnRender(Graphics paint, Rectangle cellRect, string cellValue, CellStyleInfo style, DataColumnBase column, RowColumnIndex rowColumnIndex) 
    { 
        if(string.IsNullOrEmpty(cellValue)) 
        { 
            var recordIndex = this.TableControl.ResolveToRecordIndex(rowColumnIndex.RowIndex); 
            object data = null; 
            if (recordIndex < 0) 
                return; 
            if (this.DataGrid.View.TopLevelGroup != null) 
            { 
                var record = this.DataGrid.View.TopLevelGroup.DisplayElements[recordIndex]; 
                if (!record.IsRecords) 
                    return; 
                data = (record as RecordEntry).Data; 
            } 
            else 
            { 
                data = this.DataGrid.View.Records.GetItemAt(recordIndex); 
            } 
 
            if (data != null) 
            { 
                this.DataGrid.View.GetPropertyAccessProvider().SetValue(data, column.GridColumn.MappingName, DateTime.Now); 
            } 
        } 
 
        base.OnRender(paint, cellRect, cellValue, style, column, rowColumnIndex); 
    } 
} 


Please revert to us with detail if we have misunderstood your requirement.  

Regards, 
Mohanram 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