Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have one datagridview which has one column of date data type.

Now i am using datagridview "CellFormatting" event to display date in MM-dd-YYYY format along with time

Now when i am editing date field and press tab key to go next cell.
AT that time if date value is like this "09-05-2021 02:16 PM" then it is working perfectly
and showing edited value in that cell.

if date value is like this "01-25-2021 08:32 AM" at that time it is showing String Was Not Recognized As A Valid DateTime exception.

I have check this in datagridview _DataError event.

so how to solve it ?

What I have tried:

To display date column in data gridview in MM-dd-YYYY format along with time.
CellFormatting Event


if (e.ColumnIndex == 5)
{

String value = e.Value as string;

string inputString;
DateTime dDate;

inputString = e.Value.ToString();
if (DateTime.TryParse(inputString, out dDate))
{
	e.Value = string.Format("{0:MM/dd/yyyy hh:mm tt}", dDate);
	e.FormattingApplied = true;
}
else
{
}
}



Code which will be called on gridview CellEndEdit event to convert date in format

string value2 = grvJobList.Rows[e.RowIndex].Cells[5].Value.ToString() as string;
 if (e.ColumnIndex == 5 & e.RowIndex > -1)
                {

                    if ((value2 != null) && (value2 != string.Empty))
                    {
                        string inputString = "2000-02-02";

                        DateTime dDate = DateTime.Now;

                        inputString = string.Format("{0:MM/d/yyyy}", value2);
                     
                        inputString = value2.ToString();

                        

                        if (DateTime.TryParse(inputString, out dDate))
                        {

                         
                            value2 = string.Format("{0:MM/dd/yyyy hh:mm tt}", dDate);

                            
                            grvJobList.Rows[e.RowIndex].Cells[5].Value = value2;
                            grvJobList.Rows[e.RowIndex].Cells[5].Tag = inputString;
                        }
                        else
                        {
                            grvJobList.Rows[e.RowIndex].Cells[5].Tag = inputString;


                            
                        }
                    }
                    else
                    {
                        
                    }
                }
Posted
Updated 8-Dec-21 0:23am
Comments
CHill60 8-Dec-21 5:15am    
You need to define the culture you are using (USA) - This article might help Working with Dates & Time - The complete C# tutorial[^]
Devendra Sarang 8-Dec-21 6:17am    
it can not be achieve without culture information ?

1 solution

Instead of trying to force a format on each date time value, just use the Format property of teh cell style: Format Data in DataGridView Control - Windows Forms .NET Framework | Microsoft Docs[^] The system will apply it

That way, you can keep the data as DateTime - so no need to play around with it - and display it in whatever format you or the user decide.
 
Share this answer
 
Comments
Devendra Sarang 8-Dec-21 6:36am    
@OriginalGriff

so if set

Datagridview.Columns[DateColumn].DefaultCellStyle.Format = "d";

so if i change dd-MM-yyyy to MM-dd-yyyy from system settings, it will display automatically as per the system date format.

Correct ?

no need to handle CellFormatting Event
OriginalGriff 8-Dec-21 7:11am    
That's right - the system knows what you want, so it formats it for you.
Devendra Sarang 8-Dec-21 7:04am    
because using above code, i am not getting error.

If i am showing date without time, no error

If i am showing date with time and editing then getting error
OriginalGriff 8-Dec-21 7:11am    
That's probably because you need to handle the EndEdit event and validate what has been entered - which is only sensible anyway!
Devendra Sarang 9-Dec-21 5:10am    
yes. i am handling EndEdit event. but gridview DataError is firing before cellendedit event.

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