I have a gridview with a boundfield that contains DateTime columns. In the OnRowDataBound event I check if that date is NOT NULL and depending on the result I do some action.
if (!String.IsNullOrEmpty(e.Row.Cells[16].ToString()))
BUT, even though the cell is NULL - would it be empty the date would be displayed as 1.1.1900 - the condition is always fulfilled!
I did some workaround replacing the null values with '' (empty) and therefore changed the condition into
if (!e.Row.Cells[16].Text != "01/01/1900 0:00:00")
This works, but then I have all this irrelevant data displayed in the gridview.
What's wrong?
I was asked to clarify what had been the problem for future users who might step on this thread:
Imagine you have this SQL query:
select getdate() as date
union all
select NULL
the data returned is:
2015-12-22 12:58:37.650
NULL
if you display this data in a gridview and check its values in the OnRowDataBound event, then you step on the issue I had: In order to find those rows that do not have a NULL entry you CAN NOT do:
if(e.Row.Cells[12].Text != null)
it does not work. The "nothing" that is displayed in the gridview cell in this case has 6 characters (don't ask my why - this is pure empiric) and therefore can be filtered by
if(e.Row.Cells[12].Text.Length != 6)
I implemented this in my solution, but now as do write this, I come to the conclusion that this all could have been avoided by simply specify the correct default date ("1900-01-01") in the SQL Union expression. Actually I think I stepped on something incoherent in SQL. In the above Query the NULL shouldn't be returned, as the type of the column definitely is datetime. Hope now everybody is fine with this comment. Martin