8

I am pulling data from an access database to show in a GridView control on a ASP.NET project. It works fine but I want to see if I can format the data that is being pulled. Currently any currency is being truncated from xx.xx to just the dollar amounts. Also the dates are displaying mm/dd/yyyy hh/mm/ss AM/PM

I tried editing the database itself to the right values (I set the currency field to "Currency" and the date field to "Short Date" but when I pull that date it still shows them not formatted.

EDIT: Sorry, had to take the code down

Any ideas? Thank you

SS113
  • 548
  • 1
  • 11
  • 21

5 Answers5

10

in the grid view of yours add the property called DataFormatString

DataFormatString examples:


{0:dd MMMM yyyy}    -    gives 24 February 2006
{0:MMM dd}          -    gives Feb 24 (substitue MMM with MMMM for the full month name 
                         instead of abbreviation) 
{0:dd/MM/yy}        -    gives 24/02/06 
{0:dd/MM/yyyy}      -    gives 24/02/2006

Sample Code

<asp:BoundField HeaderText="Date" 
                DataField="SampleDate" 
                DataFormatString="{0:MM/dd/yyyy}"  >

MSDN BoundField.DataFormatString Property

Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62
1

You just need to set the dataformatstring with how you want it to be populated.

As exemplified on the MSDN page:

Money:

<asp:BoundColumn HeaderText="Price" DataField="Price"
                                     DataFormatString="{0:c}" />

With the {0:c}, placing a number after the c value (such as {0:c2}) will give you that many decimal places.

Date:

<asp:boundfield datafield="MyDate" dataformatstring="{0:MM/dd/yyyy}" />
Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62
JohnP
  • 402
  • 1
  • 8
  • 25
0

One solution would be:

  <asp:GridView ID="gvLEmployees" runat="server" AutoGenerateColumns="false" >
                                <Columns>
                                    <asp:TemplateField HeaderText="Name">
                                        <ItemTemplate>

                                                <%# Eval("Name")  %>                             
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Date">
                                        <ItemTemplate>
                                             <%# Convert.ToDateTime(Eval("JoinDate")).ToShortDateString() %>
OR
<%# Convert.ToDateTime(Eval("JoinDate")).ToString("d") %>
                                        </ItemTemplate>

                                    </asp:TemplateField>
        </Column>
        </Gridview>
Irfan TahirKheli
  • 3,652
  • 1
  • 22
  • 36
0

You have to Create a BoundField, set its properties including the property dataformatstring which you can use to set format of the field appear in the GridView, and add it to the GridView control.

here below some code

 public GridView Cr_GridView(string[] ColNames,string[] ColLable, string[] ColFormat)
        {
            GridView GV = new GridView();
            int x = ColNames.GetUpperBound(0);

            for (int i = 0; i < x; i++)
            {
                BoundField GvField = new BoundField();

                GvField.DataField = ColNames[i];
                GvField.HeaderText = ColLable[i];
                GvField.DataFormatString = ColFormat[i];// for example "{0:dd/MM/yyyy}" for a date field

                GV.Columns.Add(GvField);
            }
            return GV;
        } 
Irfan TahirKheli
  • 3,652
  • 1
  • 22
  • 36
0

You can also check if you are using Template Field:

    <asp:TemplateField HeaderText="last_modified_date">
    <ItemTemplate>                           
        <asp:Label ID="lblModyDate" runat="server" Font-Size="10px" CssClass="ControlStyleUpperCase" 
Text='<%# Bind("last_modified_date","{0:dd/MM/yyyy HH:mm:tt}") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>