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
close icon

Null DateTime Column Values

If I bind a column to a DateTime property, how can I allow that column to be null? Basically, I don't want that column to remain "empty" unless the users specifically enters a date. Is there a way to do this?

8 Replies

HA haneefm Syncfusion Team June 20, 2007 06:16 PM UTC

Hi Tod,

You can use the DBNull.Value for DateTime Column to make the empty cell. Please try this code snippet and let me know if this helps.

[c#]
this.grid.Table.Records[1].SetValue("ColumnName", DBNull.Value);

Best regards,
Haneef


TG Tod Golding June 20, 2007 06:58 PM UTC

I'm not sure that will work. That allows me to manually set a specific row's value to null. I want any new row being added to have a default of null. So, I want the cell to be empty for each new row being added. How do I achieve that?



>Hi Tod,

You can use the DBNull.Value for DateTime Column to make the empty cell. Please try this code snippet and let me know if this helps.

[c#]
this.grid.Table.Records[1].SetValue("ColumnName", DBNull.Value);

Best regards,
Haneef


HA haneefm Syncfusion Team June 20, 2007 08:06 PM UTC

Hi Tod,

Please refer this thread
http://www.syncfusion.com/Support/forums/message.aspx?MessageID=37663

Best regards,
Haneef


TG Tod Golding June 20, 2007 08:34 PM UTC

This doesn't fully solve the problem. While I can now initalize the column to a null value, the cell fails when I try to add the new row because it says it can't convert from DBNull to DateTime.

>Hi Tod,

Please refer this thread
http://www.syncfusion.com/Support/forums/message.aspx?MessageID=37663

Best regards,
Haneef


HA haneefm Syncfusion Team June 21, 2007 12:13 AM UTC

Hi Tod,

Here is a minimal sample that shows you "How to set the default value in a AddNewRecord using CurrentContextChange event of the grid?". Please try this and let me know if this helps.
AddNewDefaultValue.zip

Best regards,
Haneef


TG Tod Golding June 21, 2007 05:59 AM UTC

Your example relies on a DataTable as a source (which understands how to process conversion of the null value). Try this with a binding to a custom object that has a DateTime value as one of its properties. Then, as best I can tell, the coversion fails.

>Hi Tod,

Here is a minimal sample that shows you "How to set the default value in a AddNewRecord using CurrentContextChange event of the grid?". Please try this and let me know if this helps.
AddNewDefaultValue.zip

Best regards,
Haneef


TG Tod Golding June 21, 2007 05:59 AM UTC

Your example relies on a DataTable as a source (which understands how to process conversion of the null value). Try this with a binding to a custom object that has a DateTime value as one of its properties. Then, as best I can tell, the coversion fails.

>Hi Tod,

Here is a minimal sample that shows you "How to set the default value in a AddNewRecord using CurrentContextChange event of the grid?". Please try this and let me know if this helps.
AddNewDefaultValue.zip

Best regards,
Haneef


HA haneefm Syncfusion Team June 21, 2007 06:00 PM UTC

Hi Tod,

You can make a DateTime column of your custom collection as a Nullable column. Below are the code snippet for setting the nullable column.

private DateTime? date;
public DateTime? Date
{
get
{return this.date;}
set
{ this.date = value; }
}

Please refer to the attached sample for implementation and let me know if this helps.
ModifiedAddNewDefaultValue.zip

Best regards,
Haneef

Loader.
Live Chat Icon For mobile
Up arrow icon