UNANSWERED: how to insert a null value in a datagrid datetime field

T

Tim Mackey

hi,
i found 2 unanswered posts for this problem by William Oliver on 5 Aug 2002
and on 29 Aug 2002 by Fabio Maresca.

here is our problem:
i have a datagrid, with a datetime datacolumn. when i delete the value in
the cell and then click outside the cell, it inserts 01/01/0001.
i have AllowNull set to true on the column, and the default value is DBNull.

i would appreciate if anyone knows a workaround, or if this is a big bug
then hopefully .net 2.0 can include a fix for it.

thank you
tim

\\ blog: http://tim.mackey.ie //
67d0ebfec70e8db3
 
T

Tim Mackey

as a user of the winforms app, i click in the cell in the datagrid and press
the delete key. the value disappears, but once the focus leaves the cell,
the date 01/01/0001 appears in the cell. ideally, i want it to appear empty
to the user.

thanks
tim
 
M

Marc Scheuner [MVP ADSI]

here is our problem:
i have a datagrid, with a datetime datacolumn. when i delete the value in
the cell and then click outside the cell, it inserts 01/01/0001.
i have AllowNull set to true on the column, and the default value is DBNull.

You can't set a DateTime to "null" - the DateTime basically is a
numerical value in the background, and it doesn't know the concept of
"NULL" per se.

What you can do (but it requires some work and coding on your part) is
set the DateTime object of yours to "DateTime.MinValue" and when
passing that back to the database, check for "DateTime.MinValue" and
if it's that value, set your database field to NULL.

Marc

================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
T

Tim Mackey

hi Marc,
thanks for the reply. there's got to be a better way?! surely since
databases accept null values in datetime fields, the datagrid should too?
i actually can have a datagrid with null values in a cell that is typed as
datetime.
see screenshot on
http://www.ibenza.com/Dev/datagrid_null_date_values_screenshot.jpg
the problem is, as soon as some date is in there, it defaults back to
1/1/0001 when you try to delete it.
 
M

Marc Scheuner [MVP ADSI]

thanks for the reply. there's got to be a better way?! surely since
databases accept null values in datetime fields, the datagrid should too?

If you have a data grid that can handle that (e.g. interpret the
"DateTime.MinValue" as a "null date") - the default .NET DataGrid
can't AFAIK. It will get the DateTime field, see that there is a value
in it (the DateTime.MinValue), and then display the date this
corresponds to. You'd have to be able to tell your DataGrid to display
(null) whenever it finds a DateTime.MinValue entry.

Marc

================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
T

Tim_Mac

hi marc,
my datagrid has columns that can be dynamically changed. so when i add
a new datetime column, all the rows for it fill with (null).
i suppose this is a fluke rather than a feature for the datagrid
control?
i guess i'll have to go with your suggestion of using the Min Value.
thanks for your input.
tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top