Showing date in MM/DD/YY format in a gridview column from .cs page/rowdatabound?

我们两清 提交于 2019-12-24 05:13:13

问题


I am getting a date time field from database with timestamp.

My grid has an auto-generated column.

I am formatting it in my .cs page

dsWorkItems.Tables[0].Rows[count]["Date"] =
                    !string.IsNullOrEmpty(dsWorkItems.Tables[0].Rows[count]["Date"].ToString()) ?
                    ((DateTime)(dsWorkItems.Tables[0].Rows[count]["Date"])).ToShortDateString(): "";

In quick watch I am getting

((DateTime)(dsWorkItems.Tables[0].Rows[count]["Date"])).ToShortDateString() as 9/26/2013  

but in grid I am getting 9/26/2013 12:00:00 AM but I want to show in grid 9/26/13

And one more thing is it possible to show the full datetimestamp value from database in cell tooltip..

I have tried

 dsWorkItems.Tables[0].Columns["Date"].DefaultCellStyle.Format = "MM/dd/yy";

but DefaultCellStyle assembly missing i added System.Windows.Form but still not worked


回答1:


Method 1: In aspx page.

The best thing to do in this case that I prefer to do is:

<asp:BoundField DataField="Modified" DataFormatString="{0:d}" 
   HeaderText="Modified On" HeaderStyle-BackColor="#D9EDF7"></asp:BoundField>

and set the AutoGenerateColumns="false" in the gridview.

Use BoundField for all the columns.

Method 2: In Codebehind have a row-databound event for your grid and perform the necessary formatting like this:

date1.ToString("d", DateTimeFormatInfo.InvariantInfo);

Cheers!!



来源:https://stackoverflow.com/questions/19045385/showing-date-in-mm-dd-yy-format-in-a-gridview-column-from-cs-page-rowdatabound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!