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

C#
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;
}
}
}
Vishal JoshiRank #377 · 6.7K reputationPosted Apr 29, 2024, 3:01 AM EDT
Hello Mehmet Faith
You can use PadLeft function of c#.
Please try the below sample code to add zero before day and month if it's in single digit.
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.ToString().PadLeft(2, '0')); //"01" dateString.Append("/"); dateString.Append(theDate.Month.ToString().PadLeft(2, '0')); //"01" 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; } } }Thanks
Vishal Joshi
Mehmet FatihRank #1,474 · 1.1K reputationPosted Apr 28, 2024, 7:30 AM EDT
Thanks Jignesh Kumar I tried both codes but both give the error shown in the picture.
Jignesh KumarRank #91 · 22.9K reputationPosted Apr 29, 2024, 1:35 AM EDT
Hello Mehmet,
Please use this way and check column is date column or not,
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { // If the column is the YourDateColumnName column, check the // value. if (this.dataGridView1.Columns[e.ColumnIndex].Name == "YourDateColumnName") { ShortFormDateFormat(e); } }private static void ShortFormDateFormat(DataGridViewCellFormattingEventArgs formatting) { if (formatting.Value != null) { try { DateTime theDate = DateTime.Parse(formatting.Value.ToString()); String dateString = theDate.ToString("dd-MM-yyyy"); formatting.Value = dateString; formatting.FormattingApplied = true; } catch (FormatException) { // Set to false in case there are other handlers interested trying to format this // DataGridViewCellFormattingEventArgs instance. formatting.FormattingApplied = false; } } }Jignesh KumarRank #91 · 22.9K reputationPosted Apr 28, 2024, 6:30 AM EDT
Hello,
You need to add a format for your date, You no need to write all code for that you can use below one,
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs formatting) { if (formatting.Value != null) { try { formatting.Value = formatting.Value.ToString("dd/MM/yyyy"); formatting.FormattingApplied = true; } catch (FormatException) { // Set to false in case there are other handlers interested trying to // format this DataGridViewCellFormattingEventArgs instance. formatting.FormattingApplied = false; } } }if not work then please do below,
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("dd/MM/yyyy"); formatting.FormattingApplied = true; } catch (FormatException) { // Set to false in case there are other handlers interested trying to // format this DataGridViewCellFormattingEventArgs instance. formatting.FormattingApplied = false; } } }Mehmet FatihRank #1,474 · 1.1K reputationPosted Apr 29, 2024, 3:38 AM EDT
Thanks Vishal Joshi, It worked for me.