I'm selecting value from my database, passing it to datatable then giving DataGridView.DataSource value of datatable.
The thing is that in those rows i'm also selecting some dates and problem is that my default date in database is '1900-01-01 00:00:00' because i use "smalldatetime".
And what i want is this...when I find default value in database i want to show it like an empty string, like i said...because date is in smalldatetime format i can't do this in SQL query so i wonder how can I change this in DataTable or even better in DataGridView...of course, in code? Thanks...

7 Years
Discussion Span
Last Post by TomW

You can set the datetime column of the datatable to allow nulls but it wont accept blank strings. However I suggest that leaving a default date is good practice, filtering such as in a where clause on or against null values prevents the benefits of table indexing.


Ok, this is for accessing DataGridView value:

DataGridView1.Item(column, row).Value

Didn't know it before...


I seem to have answered your post question but not in regards to the thread title. A DataGridView is a control that provides a way to display the data of its underlying datasource. You should think of a DGV as a viewer and always work with the actual data which is the actual datasource (datatable).


As for suggestions on how to handle this, I guess a lot depends on whether this particular datatable is going to update existing records or insert new records back to your database? You dont want to overwrite your existing default dates with null values. If this is going to be a read only display (not updating or inserting the dv) it wont matter if you change the values in the datatable. However if you will be updating this datasource you want to take care not to lose those original default dates and/or replace them back before updating your db.

My suggestion would be to have two columns, one which is your datatime column holding the true values but hidden from the user and an additional (new) column that displays a string formatted representation of those dates for the user.

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.