Mervin
Mervin

Reputation: 725

How can you show the date in a gridview as a date only instead of a datetime?

I am pulling in date values from a sql server database using a gridview and and the date gets converted from

12/12/2009 to 12/12/2009 12:00:00 AM

How do I prevent that from happening ?

Thanks !

Upvotes: 22

Views: 66754

Answers (10)

manikandan
manikandan

Reputation: 1

for (int j = 0; j < gv_bill_dmd_process_create.Rows.Count; j++)
                {
                    GridViewRow row_fees = (GridViewRow)gv_bill_dmd_process_create.Rows[j];
                    TextBox gv_chk_bill_dept = row_fees.FindControl("txt_gv_DmdProsDuedate") as TextBox;
                    AjaxControlToolkit.CalendarExtender gv_chk_bill_dept1 = row_fees.FindControl("txt_gv_DmdProsDuedate_CalendarExtender") as AjaxControlToolkit.CalendarExtender;
                    gv_chk_bill_dept1.StartDate = fromdate;
                    gv_chk_bill_dept1.EndDate = todate;
                }

Upvotes: -1

Ken Pespisa
Ken Pespisa

Reputation: 22266

You can use the ToString() method with a mask:

ToString("MM/dd/yyyy");

UPDATE: Just realized it would be easier in your case to do this in the grid view template

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

Upvotes: 47

Ernest
Ernest

Reputation: 1468

Within

asp:Label runat="server" Text='<%# Eval("DateAndTime") %>'

Try adding "{0:M-dd-yyyy}"

asp:Label runat="server" Text='<%# Eval("DateAndTime", "{0:M-dd-yyyy}") %>'

Upvotes: 4

ARIJIT DAS
ARIJIT DAS

Reputation: 1

Try the below code:

    <asp:BoundField DataField="my_date" HeaderText="Date" 
                            ReadOnly="True" SortExpression="my_date" 
                            DataFormatString="{0:d}" />

In the above mentioned code, my_date is the date column of the sqlserver table. The DataFormatString="{0:d}" is the main portion of this code to resolve the particular issue of yours.

Upvotes: 0

Sk Maniruddin
Sk Maniruddin

Reputation: 1

use this in query where you are getting date field

CONVERT(VARCHAR,date column name,103) as date

ex:select column1,column2,CONVERT(VARCHAR,date column name,103) as date from tablename

Upvotes: -2

tdykstra
tdykstra

Reputation: 6050

You can use a DataAnnotations attribute and a DynamicField control; then you don't have to do the same formatting every time you want to format that field. There is an example showing how to do this here: http://www.asp.net/entity-framework/tutorials/the-entity-framework-and-aspnet-%E2%80%93-getting-started-part-8

Upvotes: 1

consoleart
consoleart

Reputation: 151

You can set the date format in the bound column like this

<itemtemplate>
<asp id="Label1" runat="server" Label.Text='<%# Bind("YourDateField", "{0:M-dd-yyyy}") %>'>;
</asp>
</itemtemplate>

Upvotes: 14

Vijay
Vijay

Reputation: 446

set the dataformatstring value to "{0:d}"

Ex:

<asp:BoundField HeaderText="Date" DataField="Date_Field" ReadOnly="True" DataFormatString="{0:d}">
</asp:BoundField>

Upvotes: 5

Josh Mein
Josh Mein

Reputation: 28645

You can also use .ToShortDateString() on the DateTime Object if you are already manipulating the date in the RowDataBound

Upvotes: 2

Avitus
Avitus

Reputation: 15958

when you select the field from the database you can convert it to a string in the select as:

convert(varchar, myDate, 101)

Upvotes: 0

Related Questions