Click here to Skip to main content
11,410,174 members (61,888 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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
0 Sergey Alexandrovich Kryukov 159
1 Maciej Los 68
2 Philippe Mori 60
3 BillWoodruff 60
4 OriginalGriff 60
0 Sergey Alexandrovich Kryukov 8,755
1 OriginalGriff 6,915
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web04 | 2.8.150414.5 | Last Updated 9 Aug 2013
Copyright © CodeProject, 1999-2015
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