开发者

How to set only Date in DataTable Column of Type DateTime WEB

How to set only Date in DataTable Column of Type DateTime WEB

Hi All,

My Question is simple but please keep in mind I'm not binding it to any grid or any of ASP.NET control I have my own grid control and I want to keep it as a DateTime Column for sorting purpose.

I'm creating DataTable With Column Type DateTime.

DataTable data = new DataTable();
data.Columns.Add("Invoice Date", typeof(DateTime));
DataRow dr = data.NewRow();
dr[0] = 开发者_StackOverflow社区DateTime.Now;

//Adding filled row to the DataTable object
dataTable.Rows.Add(dr);

When the Value is shown on ASP.NET page it is show something like this:

"2/28/2011 12:00:00 AM"

I have 2 columns like this, In 1 column I want to show just date and in other column i want to show date as "Dec 2011", these formats can be achieved if i use DataColumn with type string but in that case sorting is not working properly.

Please help.

Thanks.


First of all take only date part

dr[0] = DateTime.Now.Date;

Second make use of

DateTime.ToShortDateString

    <asp:Label ID="Label1" runat="server" Text='<%# 
((DateTime)Eval("ItemValue")).ToShortDateString() %>'></asp:Label>

method to when binding value

EDIT

Not Sure but you can check this answer may help you to resolve your issue : Is it possible to format a date column of a datatable?


You seem to be mixing your concepts. The purpose of the database, and the table inside it, is to store data---not to format it. It is an implementation detail exactly how this gets stored. Just like you wouldn't care whether the datatable uses UTF-8 or UTF-16 for storing your strings, you shouldn't care what format it stores a date in. In this case, it is almost certainly stored as the number of nanoseconds since 12:00 midnight, January 1, 0001 C.E. That is, if you actually dug around in the binary format of the database, you would find those nanoseconds.

Now, the purpose of your UI layer, i.e. the ASP.NET page, is to display the data. This is where you make any formatting decisions.

One UI layer is whatever software you're using to view the database. That software is converting some number of nanoseconds, which is the internal storage format, into the string "2/28/2011 12:00:00 AM". The reason it is doing this is because that is the default format for displaying points in time.

The UI layer you are coding---the one you have control over---now needs to choose how it converts those nanoseconds into text. If it goes with the default, it will display "2/28/2011 12:00:00 AM". But if you don't want the default, you need to actually specify that. Pranay Rana's answer gives an idea of how to do that. This is the whole point of your UI layer.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜