ObjectListView sorting on date - c#

In my ObjectListView I have a column that is a date format, using {0:d/M HH:mm} as the date format. When I click the column header it sorts numerically and not by the date,
e.g.
1/2/2013
2/10/2013
3/1/2013
Instead of
2/10/2013
1/2/2013
3/1/2013
How can I set this correctly?

I cannot reproduce your issue. My ObjectListView (version 2.6.0) sorts dates correctly.
In fact, despite what #Victor says, on the documentation you can read:
ObjectListView will automatically sort the rows when the user clicks on a column header. This sorting understands the data type of the column, so sorting is always correct according to the data type. Sorting does not use the string representation.

By default ObjectListView makes ordering using String data type. Try to use explicit data type for this column in your code, e.g.:
olvColumn.DataType = typeof (DateTime);
Or add custom sorting like in this question

Related

Sorting dates in DataGridView

I'm pulling different info from a SQL table using sqlDataReader.
One of the columns contains DateTime, and is pulled from the sql and displayed correctly, using:
var datestring = reader.GetDateTime(reader.GetOrdinal("deadline")).ToShortDateString();
It is shown in the DataGridView in the following format: dd.MM.yyyy
When I try to sort this, it sorts it after dd, no matter what the MM and yyyy info is.
The datestring is put in the datagridview with the following command:
dgv_tasks.Rows.Add(tid, taskname, sidvariable, datestring);
Anyone knows why it's not sorting this as dates at all, just the number in dd ?
(I've looked here for an answer, but none of the similar questions had a working answer).
You want to leave it as a DateTime in the DataGridView, not convert it to a string. That's why it's sorting alphabetically, instead of by date.
Remove .ToShortDateString() and define your DataGridViewColumn as a DateTime type (assuming you're creating the columns manually).
After assigning the data to the DataGridView, just modify that column's DefaultCellStyle.Format value to display the date in whatever you manner you choose:
dgv_tasks.Columns["deadline"].DefaultCellStyle.Format = "dd.MM.yyyy";
It's sorting alphabetically because you converted it to a string.
Try using the date value in your datagridview with a formatter for displaying purposes.

sorting a data table column containing timestamp with time zone

I have a grid that is populated using an Oracle 11g query that returns a TIMESTAMP(6) WITH TIME ZONE field along with other fields.
When I select a date range for data to be displayed that goes from 12/26/2014 to 1/5/2015, and then try to sort by this column (asc or desc), it does not sort properly. For example, in desc order it displays from 01/01 to 01/05 and then from 12/26 to 12/31. Looks like string sorting.
I am guessing a TIMESTAMP(6) WITH TIME ZONE field that contains a value like 21-JAN-2015 18:17:16:00000 USA/EASTERN is not recognized as a date-time but rather a string. Is there any way to resolve this issue?
Turning a comment into an answer:
There is a number of options:
(i) Rely on the database to sort the data correctly - which you might want to prevent for performance reasons.
(ii) Instruct the grid to handle the timestamp column as such (and not as a string column) - which might not be possible.
(iia) Use a different grid component, which handles date columns properly.
(iii) Use a hidden (string) column with the timestamp formatted as 'YYYY-MM-DD HH24…' for sorting.
(iv) Write your own sort routine.
Feel free to provide more detail if and as you see fit.

Computed Column Based on Other Columns

I have a Dataset with a Bunch of Columns used in a Report (DevExpress XtraReports) (DataSet being the DataSource). The Dataset has many columns, and i need to read a column (based on the row type), and decide which column value to read for the row, and apply formatting based on the row type.
Example:
DataSet
DataType IntValue RealValue StringValue DateValue
Int32 123
DateTime 1/1/2011 1:23 AM
String XYZ
...
If the Datatype is DateTime, i need to read the DateValue column value, etc
I know we can use DataSet Expressions on computed columns, but cant find a way to apply the required Expression, and Format Data for the Report.
Is there a suggested way to handle this in the Report or at DataSet Level (excepting the formatting part)?
I'm not sure why you are taking this approach. This dataset is filled from a SQL datasource or some sort of backend? if that is the case, why don't you just get the computed column in the right format from the SQL directly? Wouldn't be a performance overhead to loop through all records to try and compute the "right" column?
Since it is a report, wouldn't displaying the data be enough? if you really need to use the type, you can bring that from sql as well, so you end up only with two columsn, type and data. If you can elaborate a little bit more on what you are trying to do, it will be helpful.

Is it possible to format a date column of a datatable?

Consider i have a datatable dt and it has a column DateofOrder,
DateofOrder
07/01/2010
07/05/2010
07/06/2010
I want to format these dates in DateOfOrder column to this
DateofOrder
01/Jul/2010
05/Jul/2010
06/Jul/2010
Any suggestion..
The smartest thing to do would be to make sure your DataTable is typed, and this column is of type DateTime. Then when you go to actually print the values to the screen, you can set the format at that point without mucking with the underlying data.
If that's not feasible, here's an extension method I use often:
public static void Convert<T>(this DataColumn column, Func<object, T> conversion)
{
foreach(DataRow row in column.Table.Rows)
{
row[column] = conversion(row[column]);
}
}
You could use in your situation like:
myTable.Columns["DateOfOrder"].Convert(
val => DateTime.Parse(val.ToString()).ToString("dd/MMM/yyyy"));
It only works on untyped DataTables (e.g. the column type needs to be object, or possibly string).
.ToString("dd/MMM/yyyy")
(assuming your data is DateTime type)
Just expounding on saille's answer here:
For a DateTime, format isn't an issue. A DateTime is actually the number of ticks counting up from midnight, January 1, 1 A.D. So, really, it's just a long. Formatting only becomes an issue when it comes time to convert it into a string. So, you'll have to go and take care of the formatting either when you pull it out of the data table and are ready to output it, or put it into the data table as a string (which I would not recommend, for flexibility purposes). The formatting can be done with the .ToString call on the DateTime that saille suggests, .ToString("dd/MMM/yyyy")
Just to add, you need to put your extension method to a .cs file usually some sort of a Utility.cs and then get access to that with the help of using statement.
A complete example is provided here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

DataView Sort - C#/ASP.NET

I am using the DataView.Sort to sort a column in the dataset. By default, does this assume the data is of type string when it does a sort. If so, how do I make it be datatype aware?
EDIT: How does it infer the datatype? I am creating a dataset from scratch
.Sort will honor the datatype of the column. Now, if you've already converted the column to a string type for display you have a problem. But if it's still a datetime then you're okay.

Categories

Resources