Replacing a DateTime.MinValue in a DataGridView

血红的双手。 提交于 2019-12-30 18:30:08

问题


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.


回答1:


You just need to cast it to DateTime

if ((DateTime)(resultsGrid.CurrentRow.Cells["DateOfBirth"].Value) == DateTime.MinValue)
            {
                 // Set cell value to ""
            }



回答2:


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!




回答3:


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.




回答4:


Use nullabe DateTime format instead of DateTime ("DateTime?") - don't use string.

example:

public DateTime? mydate;

and then:

DGview.Rows.Add(mydate.HasValue ? mydate : null);



回答5:


if (yourDateOfBirth != null)
{
    Convert.ToDate(yourDateOfBirth) == DateTime.MinValue
}



回答6:


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



回答7:


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.




回答8:


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


来源:https://stackoverflow.com/questions/568483/replacing-a-datetime-minvalue-in-a-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!