Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My default format is dd/MM/yyyy
After binding it to grid the format changes to MM/dd/yyyy
Posted
Comments
ccalma 25-Apr-14 2:43am    
Are you using BoundField?
JoCodes 25-Apr-14 2:47am    
Add the markup code for the gridview to the Question

Try this,if you are using bound fields:
C#
<%# Bind("date", "{0:dd/MM/yyyy}") %>>

Or
To make it more flexible, make a function and pass the desired format you want.
C#
public String DateToString(DateTime date, Int32 dateFormat)
{
    String result = "";
    if (dateFormat == 1)
        result = date.ToString("MM/dd/yyyy");
    else
        result = date.ToString("dd/MM/yyyy");

    return result;

}

On Grid view's RowDataBound event you can pass specify date format.
C#
txtDate.Text = DateToString(Convert.ToDateTime(row["Date"].ToString()), DateFormat);

Hope this will help.
Thanks
 
Share this answer
 
v2
Grid binds the data from the database which is in the format MM/dd/yyyy , you can use the select statement in the stored procedure as "convert(VARCHAR(10), DateColumn,103) AS Coulumnname" ....Also you can change format by changing 103 to 101,102,104,105 etc
 
Share this answer
 
v2

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