Output and sort the columns of the DataGrid in the format of the specified time and date.

Source: Internet
Author: User
Tags time and date

This is a small problem today: If you want a column in the DataGrid to display only the date format, for example, 2006-1-1, but this column needs to be sorted by time format when you click the header (the built-in type in the DataGrid ), at the beginning, my two methods cannot be completely different.

(1) Let the DataGrid autogeneratecolumns = true and automatically generate a new column from the datatable. datetype is datetime in chronological order, but the date display is 0:00:00.
(2) Let the DataGrid autogeneratecolumns = false, manually add the boundcolumn to the DataGrid, and control the format of dataformatstring to {0: d}. The date format is good, but the click header sorting does not work?

After a while, I found myself mentally retarded and tried the following two solutions:
(3) Let the DataGrid autogeneratecolumns = true, but use an intermediate able to change columns originally of the datetime type to the string type, change the line content to tostring ("yyyy-mm-dd") to display the time string with leading 0. in this way, the Sorting Problem will naturally be solved.
(4) find the reason why the above (2) does not work, because I dynamically add columns. When I click the header to sort PostBack, I did not add columns again.CodeIn this way, viewstate cannot be sorted for me.

In contrast, I chose (3) because I don't need to add columns by myself. I am too lazy, and I need an intermediate datatable for data access.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.