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.