Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I want to change the format of the date field binded in a grid based upon the language selection.

e.g: If the language is english/ USEnglish then the format should be "dd/MM/yyyy" and if the language is something else then the date format should be "MM/dd/yyyy". Please suggest me how to do this.

Thanks
Posted

1 solution

HI all,

I found the solution as follows:

aspx grid code:
XML
<asp:TemplateField HeaderText="" ControlStyle-Width="20px" ItemStyle-BackColor="#FFFFFF" ItemStyle-Font-Names="Verdana">
                            
                            <ItemTemplate>
                            <asp:Label ID="lblDateTimeSent" runat="server" Text='<%# FormatDate(Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "DateTimeSent"))) %>' style="text-align:center;"></asp:Label>
                            </ItemTemplate>
                            </asp:TemplateField>


Code behind code:
C#
public string FormatDate(DateTime input)
        {
            //String languageID = Convert.ToString(Session["LanguageId"]);
            if (languageId == int.Parse(System.Configuration.ConfigurationManager.AppSettings["DefaultLanguageID"]))
            {
                return string.Format("{0:MM/dd/yy}", input);
            }
            else
            {
                return string.Format("{0:dd/MM/yy}", input);
            }
        }


This above solution will be helpful to all but this will not work with the bound field. If there are bound fields then we need to use the item template instead.

Thanks
 
Share this answer
 

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