Replacing DateTime.MinValue in a DataGridView

I am working on a name recording 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 DATETIME. The original Access database that I migrated to the SQLite database was nullable for the date of birth, so when I copied it, I set all the null values ​​to DateTime.MinValue.

In my application, the date of birth column is formatted as follows:

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 in rows where there is no date of birth, the database has DateTime.MinValue, which appears in my DataGridView as 01/01/0001.

I am looking for a way to replace 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 "" } } } 

Does anyone have an idea how I can replace DateTime.MinValue in my DataGridView with an empty row? Thanks!

Edit: returning a DateTime cell value allows the if statement to work, which I should work on, but I'm still not able to calculate the encoding to replace MinValues ​​with an empty string.

+7
c # datetime sqlite datagridview
source share
8 answers

You just need to send it to DateTime

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

I found why DateTime.MinValue displayed!

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

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

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

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

+4
source share

Although it may be quite simple for you to achieve the necessary conversion (see the excellent answers that preceded mine), I believe that you should ideally replace all DateTime.MinValue values ​​in this particular column in the database with NULL . This correctly reflects the actual lack of data in this position. Your database currently contains invalid data and you are trying to process the data for display.

Therefore, you can handle the display of data in your GridView using the EmptyDataText or NullDisplayText properties to provide appropriate rendering.

+2
source share

Use Dateab format in nullabe format instead of DateTime ("DateTime?") - do not use string.

Example:

 public DateTime? mydate; 

and then:

 DGview.Rows.Add(mydate.HasValue ? mydate : null); 
+2
source share
 if (yourDateOfBirth != null) { Convert.ToDate(yourDateOfBirth) == DateTime.MinValue } 
0
source share

you can pass an 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 
0
source share
 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 may have to drop DateTime and then compare with MinValue, but it looks like you have the right idea.

0
source share

There are already answers that should achieve what you want; but I wonder why you are not putting NULL in the database to get started? In sqlite3, β€œdatetime” is more or less a simple string (there is nothing special in the datetime column, since sqlite3 is just an afinity type, not a type binding), so you just insert NULL into that column. And if you want to get MinValue datetime on request, you can easily execute a query like

 select coalesce(your_datetime_column,'01/01/0001') from your_table 
0
source share

All Articles