How to display only the date in the GridView when retrieving data from the database? WITH#

I am retrieving data from an access database for display in a GridView control in an ASP.NET project. It works fine, but I want to see if I can format the data that it pulls out. Currently, any currency is truncated from xx.xx to the amount in dollars. Also the dates show mm / dd / yyyy hh / mm / ss AM / PM

I tried to change the database to the correct values ​​(I set the currency field to Currency and the date field to Short Date, but when I pulled out this date, it still shows that they are not formatted.

EDIT: Sorry, you had to leave a code

Any ideas? thank you

+6
source share
4 answers

in the grid view add the DataFormatString property

Examples of DataFormatString:

 {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 02/24/2006

Code example

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

MSDN Property BoundField.DataFormatString

+7
source

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

As shown on the MSDN page:

Money:

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

Using {0: c}, placing the number after the value of c (for example, {0: c2}) will give you many decimal places.

The date:

 <asp:boundfield datafield="MyDate" dataformatstring="{0:MM/dd/yyyy}" /> 
+1
source

One solution:

  <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> 
0
source

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

here below 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; } 
0
source

All Articles