views:

1666

answers:

7

I am working on a name record application and the information is stored in a SQLite database. All columns in the database are TEXT types, except for the date of birth column, which is a DATETIME. The original Access database that I transferred to the SQLite database allowed nulls for the date of birth, so when I copied it over, I set all nulls to DateTime.MinValue.

In my application, the date of birth column is formatted like so:

DataGridViewTextBoxColumn dateOfBirth = new DataGridViewTextBoxColumn();
        dateOfBirth.HeaderText = "DOB";
        dateOfBirth.DataPropertyName = "DateOfBirth";
        dateOfBirth.Width = 75;
        dateOfBirth.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        dateOfBirth.DefaultCellStyle.Format = "MM/dd/yyyy";

My problem is that rows where there is not a date of birth, the database has DateTime.MinValue, which displays in my DataGridView as 01/01/0001.

I am looking for a way to replace the 01/01/0001 with an empty string ("") in my DataGridView.

private void resultsGrid_DateFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
    {
        if(resultsGrid.Columns[e.ColumnIndex].Name.Equals("DateOfBirth"))
        {
            if ((DateTime)(resultsGrid.CurrentRow.Cells["DateOfBirth"].Value) == DateTime.MinValue)
            {
                 // Set cell value to ""
            }
        }
    }

Anyone have an idea how I can replace a DateTime.MinValue in my DataGridView with an empty string? Thanks!

Edit: Casting the cell value with DateTime allows the if statement I have to work, but I am still not able to figure out the coding to replace the MinValues with a blank string.

+3  A: 

You just need to cast it to DateTime

if ((DateTime)(resultsGrid.CurrentRow.Cells["DateOfBirth"].Value) == DateTime.MinValue)
            {
                 // Set cell value to ""
            }
Barbaros Alp
I added your change to my code above, and it does allow my if statement to work, but I still can't figure the code to actually change the cell contents.
Jared Harley
In the if statement where you put // Set cell value to ""; i guess you need to write this code resultsGrid.CurrentRow.Cells["DateOfBirth"].Value = ""It should do it
Barbaros Alp
resultsGrid.CurrentRow.Cells["DateOfBirth"].Value = ""
Barbaros Alp
A: 
if (yourDateOfBirth != null)
{
    Convert.ToDate(yourDateOfBirth) == DateTime.MinValue
}
Chris Ballance
A: 

you can cast the object as a DateTime object:

//create a nullable intermediate variable
System.DateTime? dtCellValue = resultsGrid.CurrentRow.Cells["DateOfBirth"].Value as System.DateTime?;

//if this cell has been set to String.Emtpy the cast will fail
//so ensure the intermediate variable isn't null
if ( dtCellValue.HasValue && dtCellValue == System.DateTime.MinValue )
//set cell value appropriately
Jason
A: 
private void resultsGrid_DateFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
    if(resultsGrid.Columns[e.ColumnIndex].Name.Equals("DateOfBirth"))
{
    if ((string)resultsGrid.CurrentRow.Cells["DateOfBirth"].Value == DateTime.MinValue.ToString())
    {
         // Set cell value to ""
    }
}

}

Or you might have to cast to DateTime and then compare to MinValue, but it sounds like you have the right idea.

mletterle
A: 

While it may be quite straightforward to achieve the transformation you need (see the excellent answers preceding mine), I feel that you should ideally replace all the DateTime.MinValue values in this particular column in the database with NULL. This would correctly represent the actual lack of data in this position. At present your database contains incorrect data and you are trying to post-process the data for display.

Consequently, you would be able to handle the display of data in your GridView using the EmptyDataText or NullDisplayText properties to provide appropriate rendering.

Cerebrus
I'm brand new to working with SQLite, so I could be wrong, but I thought that a DateTime couldn't be null. If it can, that would be the optimal solution, because you're right - I've actually put incorrect data into my database.
Jared Harley
Unfortunately, I haven't worked with SQLite, and know even less than you might. However, I think it's worth investigating. Take a look at the documentation. Their site tells me that in SQLite 2, everything was typeless. For SQLite3 - http://www.sqlite.org/datatype3.html
Cerebrus
A: 

There are already answers that should achieve what you want; but I wonder why you don't put NULL in the database to start with? In sqlite3, "datetime" is more or less just a string, (there is nothing special about a datetime column since sqlite3 is all about just type afinity, not type binding), so you just insert NULL in that column. And in case you want to have the MinValue of datetime when you query, you can easily execute a query like

select coalesce(your_datetime_column,'01/01/0001') from your_table
polyglot
+1  A: 

I discovered why the DateTime.MinValue was displaying!

This line in the cell formatting section caused the MinValue of "01/01/0001" to display:

dateOfBirth.DefaultCellStyle.Format = "MM/dd/yyyy";

Without this line, the Date of Birth field in my datagridview is left blank.

Barbaros Alp is still correct about what I was trying to do at the time. Thanks everyone!

Jared Harley