rai nalasa rai nalasa - 3 months ago 23
ASP.NET (C#) Question

remove 12:00 am time on gridview

How do I remove the time portion on gridview?I tried some answer in stack but its now working for me.

Here is my gridview

enter image description here

here is my code for date time and asp .net code as well.

var dateAndTime = DateTime.Now;
var date = dateAndTime.Date;
cmd.Parameters.AddWithValue("@date_now",date);


and html gridview mark up

<asp:GridView ID="GridView1" runat="server"
CssClass="table table-hover table-striped"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
AutoGenerateColumns="False">
<Columns>
<asp:ButtonField Text="Select" CommandName="Select" ItemStyle-Width="150">
<ItemStyle></ItemStyle>
</asp:ButtonField>
<asp:BoundField DataField="payment_date" HeaderText="Date of Payment"
SortExpression="DateField" DataFormatString="{0:MM/dd/yyyy}"/>
<asp:BoundField DataField="transaction_id" HeaderText="Trans I.D"
SortExpression="DateField" />
<asp:BoundField DataField="transaction_number" HeaderText="Transaction number"
SortExpression="DateField" />
<asp:BoundField DataField="product_id" HeaderText="Product I.D"
SortExpression="DateField" />
<asp:BoundField DataField="productName" HeaderText="Product"
SortExpression="DateField" />
</Columns>
</asp:GridView>


I didn't post the entire sql query because it's quite long.
How do I remove the time portion?

Answer

Specify the format using DataFormatString attribute.

DataFormatString="{0:MM/dd/yyyy}"

So, it should be..

<asp:BoundField DataField="payment_date" HeaderText="Date of Payment"
            SortExpression="DateField" DataFormatString="{0:MM/dd/yyyy}"/>
Comments