Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. formatting date in gridview [modified]

formatting date in gridview [modified]

Scheduled Pinned Locked Moved ASP.NET
question
14 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Kunal P

    hi i am dev a web application in visual web dev express.. wen using the gridview control i wanted that the date column be formated in (dd/MMM/yyyy) format. i m entering in the gridview EDIT ITEM option {0:dd/MMM/yyyy} this aint working. how can i do this..

    Kunal Piyush

    K Offline
    K Offline
    kubben
    wrote on last edited by
    #2

    If you are using the Eval method of binding it looks like this: <%# Eval("Date", "{0:d}")%>, If you are using a boundfield it looks like this: Finally if you use a DataBinderEval (that is what I suggest) You have this: <%# DataBinder.Eval(Container.DataItem,"Date").ToString("dd/MMM/yyyy") %> The other two options you really can't give it the dd/MMM/yyyy format. Hope that helps. Ben

    K 1 Reply Last reply
    0
    • K kubben

      If you are using the Eval method of binding it looks like this: <%# Eval("Date", "{0:d}")%>, If you are using a boundfield it looks like this: Finally if you use a DataBinderEval (that is what I suggest) You have this: <%# DataBinder.Eval(Container.DataItem,"Date").ToString("dd/MMM/yyyy") %> The other two options you really can't give it the dd/MMM/yyyy format. Hope that helps. Ben

      K Offline
      K Offline
      Kunal P
      wrote on last edited by
      #3

      kubben wrote:

      Finally if you use a DataBinderEval (that is what I suggest) You have this: <%# DataBinder.Eval(Container.DataItem,"Date").ToString("dd/MMM/yyyy") %>

      hey i tried wat you said on the page load event i wrote this DataBinder.Eval(GridView1.Columns[10], "Date").ToString("dd/MMM/yy"); i couldnt get wat COntainer.dataitem meant.. there wasnt any optio for the same.. i used columns[10], since the date column was the 10th including 0.. but i get an error.. tostring only takes 1 argument.. please help

      Kunal Piyush

      K 1 Reply Last reply
      0
      • K Kunal P

        kubben wrote:

        Finally if you use a DataBinderEval (that is what I suggest) You have this: <%# DataBinder.Eval(Container.DataItem,"Date").ToString("dd/MMM/yyyy") %>

        hey i tried wat you said on the page load event i wrote this DataBinder.Eval(GridView1.Columns[10], "Date").ToString("dd/MMM/yy"); i couldnt get wat COntainer.dataitem meant.. there wasnt any optio for the same.. i used columns[10], since the date column was the 10th including 0.. but i get an error.. tostring only takes 1 argument.. please help

        Kunal Piyush

        K Offline
        K Offline
        kubben
        wrote on last edited by
        #4

        In the gridview. You need to convert the column you want the date in to be an ItemTemplate. In the ItemTemplate you need a Label control. In the Text property of the Label control you need to put the this: <%# DataBinder.Eval(Container.DataItem,"Date").ToString("dd/MMM/yyyy") %> The page load event is not the correct place to put this code. So if you look at your gridview in the HTML mode you will find the <Columns> node YOu need code like this: <Columns> asp:TemplateField <ItemTemplate> <asp:Label ID="lbDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Date").ToString("dd/MMM/yyyy") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> NOTE the Date that comes right after the Container.DataItem is what ever your are calling your date field. Hope that helps. Ben

        K 1 Reply Last reply
        0
        • K kubben

          In the gridview. You need to convert the column you want the date in to be an ItemTemplate. In the ItemTemplate you need a Label control. In the Text property of the Label control you need to put the this: <%# DataBinder.Eval(Container.DataItem,"Date").ToString("dd/MMM/yyyy") %> The page load event is not the correct place to put this code. So if you look at your gridview in the HTML mode you will find the <Columns> node YOu need code like this: <Columns> asp:TemplateField <ItemTemplate> <asp:Label ID="lbDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Date").ToString("dd/MMM/yyyy") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> NOTE the Date that comes right after the Container.DataItem is what ever your are calling your date field. Hope that helps. Ben

          K Offline
          K Offline
          Kunal P
          wrote on last edited by
          #5

          this is wat i did.. still that error.. Error 1 No overload for method 'ToString' takes '1' arguments C:\Documents and Settings\Kunal\My Documents\Visual Studio 2005\WebSites\diag\User\viewalldoc.aspx 25 Kunal Piyush

          K 1 Reply Last reply
          0
          • K Kunal P

            this is wat i did.. still that error.. Error 1 No overload for method 'ToString' takes '1' arguments C:\Documents and Settings\Kunal\My Documents\Visual Studio 2005\WebSites\diag\User\viewalldoc.aspx 25 Kunal Piyush

            K Offline
            K Offline
            kubben
            wrote on last edited by
            #6

            Sorry I missed on thing: '<%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem,"Dt")).ToString("dd/MMM/yyyy") %>' That should work. Thanks, Ben

            K 1 Reply Last reply
            0
            • K kubben

              Sorry I missed on thing: '<%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem,"Dt")).ToString("dd/MMM/yyyy") %>' That should work. Thanks, Ben

              K Offline
              K Offline
              Kunal P
              wrote on last edited by
              #7

              hey now the program is compiling.. but again 1 problem. when the grid is displayed the "Dt" column shows the whole HTML tag, instead of the date. i am using VISUAL WEB DEVELOPER EXPRESS 2005.. is is a problem of obsolete tag.

              Kunal Piyush

              K 1 Reply Last reply
              0
              • K Kunal P

                hey now the program is compiling.. but again 1 problem. when the grid is displayed the "Dt" column shows the whole HTML tag, instead of the date. i am using VISUAL WEB DEVELOPER EXPRESS 2005.. is is a problem of obsolete tag.

                Kunal Piyush

                K Offline
                K Offline
                kubben
                wrote on last edited by
                #8

                I am guessing there is an unbalanced quote or something. Include the code and I will see if I can see anything wrong with it. Ben

                K 1 Reply Last reply
                0
                • K kubben

                  I am guessing there is an unbalanced quote or something. Include the code and I will see if I can see anything wrong with it. Ben

                  K Offline
                  K Offline
                  Kunal P
                  wrote on last edited by
                  #9

                  i am including the code.. though there shud have been an error regardin an unbalanced tag.. but still Kunal Piyush

                  K 1 Reply Last reply
                  0
                  • K Kunal P

                    i am including the code.. though there shud have been an error regardin an unbalanced tag.. but still Kunal Piyush

                    K Offline
                    K Offline
                    kubben
                    wrote on last edited by
                    #10

                    Your code: Text='<%Convert.ToDateTime(DataBinder.Eval(Gridview1.DataItem,"Dt")).ToString("dd/MMM/yyyy"))%>' Needs to be: Text='<%# Convert.ToDateTime(DataBinder.Eval(Gridview1.DataItem,"Dt")).ToString("dd/MMM/yyyy"))%>' That should do it. Ben

                    K 1 Reply Last reply
                    0
                    • K kubben

                      Your code: Text='<%Convert.ToDateTime(DataBinder.Eval(Gridview1.DataItem,"Dt")).ToString("dd/MMM/yyyy"))%>' Needs to be: Text='<%# Convert.ToDateTime(DataBinder.Eval(Gridview1.DataItem,"Dt")).ToString("dd/MMM/yyyy"))%>' That should do it. Ben

                      K Offline
                      K Offline
                      Kunal P
                      wrote on last edited by
                      #11

                      hey ben thanks a lot.. problem solved.. thanks again..

                      Kunal Piyush

                      K 1 Reply Last reply
                      0
                      • K Kunal P

                        hey ben thanks a lot.. problem solved.. thanks again..

                        Kunal Piyush

                        K Offline
                        K Offline
                        kubben
                        wrote on last edited by
                        #12

                        Sorry it took so long. Ben

                        K 1 Reply Last reply
                        0
                        • K kubben

                          Sorry it took so long. Ben

                          K Offline
                          K Offline
                          Kunal P
                          wrote on last edited by
                          #13

                          hey.. i need to be sorry... didnt take in consideration about the brackets.. also.. why did we use "container" keyword.. we were using a gridview..

                          Kunal Piyush

                          K 1 Reply Last reply
                          0
                          • K Kunal P

                            hey.. i need to be sorry... didnt take in consideration about the brackets.. also.. why did we use "container" keyword.. we were using a gridview..

                            Kunal Piyush

                            K Offline
                            K Offline
                            kubben
                            wrote on last edited by
                            #14

                            In this case the Container is referring to the Dataset or dataTable or dataview that is bound to the gridview. When you use DataBinder.Eval you always have to use container.dataitem and then pass in the field you are looking for. The Bind method does the same thing it is just behind the scenes. Ben

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups