Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a requirement to change the DATE FORMAT in the DataGridView on a button click from MM/dd/yyyy MM:hh:tt to dd/MM/yyyy hh:MM:tt and it's giving error on this line
C#
String.Format("{dd/MM/yyyy hh:MM:tt}", data);

Below is the code:

C#
Date_Format df = new Date_Format();
           df.cmbColumns.DataSource = cmbList;
           df.ShowDialog();


           string data = "";
           foreach (DataGridViewRow row in dataGridView1.Rows)
           {
               data = Convert.ToString(row.Cells["Sent Date"].Value);

               String.Format("{dd/MM/yyyy hh:MM:tt}", data);

           }
Posted
Updated 19-Jan-16 22:59pm
v4
Comments
CHill60 20-Jan-16 5:01am    
What is the value of data when you get the error?
Incidentally there is no need to use Convert.ToString - just use data = row.Cells["Sent Date"].Value.ToString();
Member 8010354 20-Jan-16 5:04am    
Still it's giving the same error near data

Error is: FormatExceptionwasnhandled
Additional Information: Input string is not in a correct format –
CHill60 20-Jan-16 5:07am    
Yes it would give the same error - I wasn't fixing your error which is why I said the word "incidentally".
I also asked for the value of the variable not the error details.
However you now have a solution posted which should work.
Member 8010354 20-Jan-16 5:32am    
Now i'm not getting any error but the date format is not changing in the datagridview.

private void button3_Click(object sender, EventArgs e)
{
Date_Format df = new Date_Format();
df.cmbColumns.DataSource = cmbList;
df.ShowDialog();

string data = "";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
data = Convert.ToString(row.Cells["Sent Date"].Value);
var result = String.Format("{0:'dd/MM/yyyy hh:MM:tt'}", data);
}
Sinisa Hajnal 20-Jan-16 6:36am    
Note that there is difference between MM and mm in dd/MM/yyyy hh:MM:tt, right now you show months in place of minutes.

1 solution

Um...
String.Format uses '{' and '}' to indicate parameters for substitution - but the parameters are indicated by a numeric index rather than being a format string and hoping they match up.
So:
C#
String.Format("{0}", data);
Is valid, but what you tried is not.
But there are no many other problems here...
Try this instead of string.Format:
C#
DateTime date = (DateTime) (row.Cells["Sent Date"].Value);
string formattedDate = date.ToString("dd/MM/yyyy hh:mm:tt");
 
Share this answer
 
Comments
Member 8010354 20-Jan-16 5:17am    
Now i changed the code as you told which is updated below:

private void button3_Click(object sender, EventArgs e)
{
Date_Format df = new Date_Format();
df.cmbColumns.DataSource = cmbList;
df.ShowDialog();

foreach (DataGridViewRow row in dataGridView1.Rows)
{
DateTime date = (DateTime)(row.Cells["Sent Date"].Value);
string formattedDate = date.ToString("dd/MM/yyyy hh:mm:tt");
}
}

Now it's giving InvalidCastException was unhandled
OriginalGriff 20-Jan-16 5:31am    
Right - so the value in your DataGridView isn't a DateTime - which probably means it's a string, which probably means that your problems go right back to your data source. At a guess, you are loading the DGV from a database and storing the date in there as a VARCHAR or NVARCHAR column, yes?
If so, then you need to change that: always store information in the most appropriate datatype as it makes everything you do later a whole lot easier and safer!
Have you looked at where you get the data from?
Member 8010354 20-Jan-16 5:42am    
Yeah i changed the code now.

private void button3_Click(object sender, EventArgs e)
{
Date_Format df = new Date_Format();
df.cmbColumns.DataSource = cmbList;
df.ShowDialog();

string data = "";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
data = Convert.ToString(row.Cells["Sent Date"].Value);
var result = String.Format("{0:'dd/MM/yyyy hh:MM:tt'}", data);
}
Now i'm not getting error but there is no change on date format column on selection of that column
OriginalGriff 20-Jan-16 5:58am    
Seriously - fix your data source rather than "patching round" the symptoms!
It makes all this go away and allows you a lot better flexibility...

But...you seem to have the wrong idea here - if you want to format a DGV column, you don't do it like that!
1) Formatting a string does not affect the original DGV data.
2) Changing the DGV data may result in unnecessary (or even incorrect) updates to your data source.
If you want to format a column in a specific style, then there is a specific property to do exactly that:
https://msdn.microsoft.com/library/f9x2790s%28v=vs.100%29.aspx?f=255&MSPPError=-2147217396
Which means you don't have to think about it any more, just setting the format means that every row is displayed correctly with no further work from you.
But...it only works if the data in the DGV is DateTime - you can't use it to "reformat" a date-based string into a different date format. See what I mean about getting your data source right? :laugh:
Please, do yourself a big favour and look at your original data: it's not normally a big change to correct it at the early stages of a project, but it gets to be more and more work as the project gets bigger and more complete.

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