Default value for Date fields in table

FreeriderUK

Well-known member
Joined
Jun 27, 2006
Messages
100
Location
London
Programming Experience
10+
Is it possible to set the default value for a Date field in the DataSet Designer to always be "today's date"?
 
Good question. I'm afraid the answer is "No" on an easy level. There are no constructors you can write, events you can hook or methods you can override such that a new row has the current date as default value in a column

You can edit the designer file, sure, but remember that changes are overwritten automatically whenever you change the dataset.

So youre down to either specifying the .DefaultValue every time you make a dataset(e.g. in a form), or doing something nasty like overriding the EndInit() method of the parent dataset (in the partial code behind the dataset), make sure you call the base EndInit() and then do some other stuff which will, upon every creation of the dataset, carry out the requested ops. Actually, I cant even guarantee that, of the 3 constructors that could be called, that one will be the one.. so your overridden EndInit might not run. Investigate which constructor is used?

ugh. Sorry! I guess MS intended for you to set defaultvalue yourself every time you make a new instance of that particualr datatable
 
So youre down to either specifying the .DefaultValue every time you make a dataset(e.g. in a form)

Thanks - that's what I've decided to do.

VB.NET:
'Set DefaultValue for DateColumn
Me.MyDataSet.Tables!MyTable.Columns("MyDateColumn").DefaultValue = Now

It seems to work OK.
 
er.. you would do better to access it in typed fashion, not untyped:

Me.MyDataSet.MyTable.MyDateColumn.DefaultValue = Now

More readable, and intellisense is more helpful
 
Back
Top