Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a winform with datagridview MS Access database. I have 2 columns linked to datetimepicker. In code I copy the date from one column to the other and the copied cell is blank. when I reopen the form the blank cell shows 12/30/1899. How can i keep it blank until I enter another date?

This is the code I use to copy from one cell to the other (button click)

C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
       {

           if (e.ColumnIndex == dataGridView1.Columns [19].Index)
           {

               {
                   dataGridView1.CurrentRow.Cells[16].Value = dataGridView1.CurrentRow.Cells[17].Value;
                   dataGridView1.CurrentRow.Cells[17].Value =  "";
               }
           }
       }
   }
   }


What I have tried:

Custom format
And
this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
this.dateTimePicker1.CustomFormat = " "; //a string with one whitespace
Posted
Updated 18-Jan-20 23:13pm
Comments
[no name] 18-Jan-20 10:33am    
Try assigning null instead of an empty string.

dataGridView1.CurrentRow.Cells[17].Value =null
 
Share this answer
 
Comments
Member 12349103 18-Jan-20 15:28pm    
It still shows 12/30/1899 when I reopen the app / database
ashid das11 21-Jan-20 12:47pm    
Can you share the screenshot, before and after
Member 12349103 25-Jan-20 10:41am    
How do I add screen shots?
Looks like you need to implement CellFormatting. The docs you can find here: DataGridViewCellFormattingEventArgs Class (System.Windows.Forms) | Microsoft Docs[^]

Your implementation will then look something like this
C#
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs formatting)
{
   if (e.ColumnIndex == 17)
   {
       // Here the decision what we like to show as blank. Depending on the situation 
       // you may need also to check for null Value 
       //
       // Additinal this gives you also the option to compare Col16 with Col17
       // on equality which means you don't need to set Col17 to null or empty string
       if ((DateTime)dataGridView1.CurrentRow.Cells[17].Value == DateTime.MinValue)
       {
           formatting.Value= "";
       }
   }
}

Better to work with column names than indices
C#
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs formatting)
{
   if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Column3")
   {
       if ((DateTime)dataGridView1.CurrentRow.Cells["Column3"].Value == DateTime.MinValue)
       {
           formatting.Value= "";
       }
   }
}
 
Share this answer
 
v3
Comments
Member 12349103 20-Jan-20 10:37am    
It still shows the same result. 12/30/1899

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900