Click here to Skip to main content
12,076,925 members (45,146 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
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 28-Feb-13 1:43am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

HI all,

I found the solution as follows:

aspx grid code:
<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:
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
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160212.1 | Last Updated 9 Aug 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100