I have a Datetime field I want to place in a grid column but I want to display the datetime value using a very special format that would not be good for sorting.
How can I display one thing and yet sort on the original datatype of the column? List Object attribute would be datetime, display value would be string on steroids.
I did the SortComparer on the column. This worked the best as I have some weird requirements regarding the display of my DateTime? variable. Thanks for the time and options.
Hi,
Since your question was about the sorting, I assumed you had already gotten the data displayed. So let's back up a bit.
What is the data type of the data in your data source?
If it's a DateTime, and you want to format the values in the grid, then you can simply bind the grid to your data source and you have a couple of ways you can go.
Can I assume that I should bind the grid to my "formatted" column of type string and them hook in via the SortComparer and actually reference the bound object's true datetime column for sorting purposes? This makes sense to me thus I will try it.
Thanks.
Using the Format property of the column is a good way to do it, assuming you can format the data how you want it using the built-in DotNet numeric formatting.
If you need to do more complex formatting and therefore need to display the dates as strings, then what I would do is use a SortComparer on the column. You could examine the string, convert it into a date and then compare based on the actual dates instead of the strings.
To set a Format to UltraGrid column
U Can follow the below one.
band.Columns["DateOfBirth"].Format = "MM/dd/yyyy hh:mm:sszzz";
For you notice, I hope it wn't affect Column Sorting at any case