Mehmet Fatih

Mehmet Fatih

  • 750
  • 941
  • 42.7k

Date format Problem

Apr 28 2024 9:27 AM

I want tu put 0 before the day and month dates. How can we modify the following codes?

       private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs formatting)
       {
           if (formatting.Value != null)
           {
               try
               {
                   System.Text.StringBuilder dateString = new System.Text.StringBuilder();
                   DateTime theDate = DateTime.Parse(formatting.Value.ToString());

                   dateString.Append(theDate.Day);
                   dateString.Append("/");
                   dateString.Append(theDate.Month);
                   dateString.Append("/");
                   dateString.Append(theDate.Year.ToString());
                   formatting.Value = dateString.ToString();
                   formatting.FormattingApplied = true;
               }
               catch (FormatException)
               {
                   // Set to false in case there are other handlers interested trying to
                   // format this DataGridViewCellFormattingEventArgs instance.
                   formatting.FormattingApplied = false;
               }
           }
       }

 


Answers (5)