开发者

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

I am 开发者_C百科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 !


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}" />


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>


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

Ex:

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


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}") %>'


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


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


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

convert(varchar, myDate, 101)


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.


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;
                }


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜