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
  1. Home
  2. Web Development
  3. ASP.NET
  4. How do I change the apperance of datetimes in a datagrid?

How do I change the apperance of datetimes in a datagrid?

Scheduled Pinned Locked Moved ASP.NET
helpquestion
3 Posts 3 Posters 0 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.
  • R Offline
    R Offline
    RickardB
    wrote on last edited by
    #1

    Hi, I am using a datagrid as the main display of my application. The datagrid is bound to a dataview. This dataview is constructed from a DataTable. The first column in the DataTable is of type DateTime. It is important for me that this column is of type DateTime since I want to be able to sort with respect to time, whatever time format is used. My problem is that I want the user to be able to customize the format in which the time is displayed in the first column. Any one who knows how I can accomplish this? I now about the DateTimeFormatInfo objects but I don't know where in the process to use them. Thanks in advance for any help. Rickard

    M D 2 Replies Last reply
    0
    • R RickardB

      Hi, I am using a datagrid as the main display of my application. The datagrid is bound to a dataview. This dataview is constructed from a DataTable. The first column in the DataTable is of type DateTime. It is important for me that this column is of type DateTime since I want to be able to sort with respect to time, whatever time format is used. My problem is that I want the user to be able to customize the format in which the time is displayed in the first column. Any one who knows how I can accomplish this? I now about the DateTimeFormatInfo objects but I don't know where in the process to use them. Thanks in advance for any help. Rickard

      M Offline
      M Offline
      Mazdak
      wrote on last edited by
      #2

      I haven't done it myself but I think it will works, if you use AutoGenerate Columns I think you can do it in ItemDataBound event, if no and you do not use AutoGenerate , in a Bound Coumn property builder there is Data Formatting Expression which I think if you play with it , it will do the works. Mazy "A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope

      1 Reply Last reply
      0
      • R RickardB

        Hi, I am using a datagrid as the main display of my application. The datagrid is bound to a dataview. This dataview is constructed from a DataTable. The first column in the DataTable is of type DateTime. It is important for me that this column is of type DateTime since I want to be able to sort with respect to time, whatever time format is used. My problem is that I want the user to be able to customize the format in which the time is displayed in the first column. Any one who knows how I can accomplish this? I now about the DateTimeFormatInfo objects but I don't know where in the process to use them. Thanks in advance for any help. Rickard

        D Offline
        D Offline
        DFU23
        wrote on last edited by
        #3

        Like Mazdak said, you can change the display of the DateTime object when it is databound to the DataGrid. Example ASPX:

        <asp:DataGrid runat="server" id="MyDataGrid" AutoGenerateColumns="false">
        <columns>
        <asp:TemplateColumn HeaderText="Date/Time">
        <ItemTemplate>
        <asp:Label ID="lblDate" Text='<%# DataBinder.Eval(Container.DataItem, "MyDate") %>' runat="server" OnDataBinding="lblDate_DataBinding" />
        <ItemTemplate>
        </asp:TemplateColumn>
        </columns>
        </asp:DataGrid>

        In the code behind:

        private void lblDate_DataBinding(object sender, System.EventArgs e) {
        Label lblDateTime = (Label)sender;
        DateTime dteDateTime = System.Convert.ToDateTime(lblDateTime.Text);
        lblDateTime.Text = dteDateTime.ToString("MMM dd YYYY");
        }

        If you want to let the user choose the format I guess you could put DropDownList on the page with available formats and whenever that changes grab the format string to use it in the ToString method of the DateTime object. Should work. (?) Let me know.

        Wally Atkins
        Newport News, VA, USA
        http://wallyatkins.com

        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