DataView Sort - C#/ASP.NET - c#

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.

Related

C# SQL DataTable: adding a column of custom type

I want to call a stored procedure on my SQL database (MSSQL 2008 R2) using a table-valued parameter. I can do that fine with something like
var table = new DataTable();
table.Columns.Add(new DataColumn("Col1", typeof(int)));
table.Columns.Add(new DataColumn("Col2", typeof(string)));
table.Rows.Add(55, "hello");
table.Rows.Add(66, "goodbye");
Then later on I construct an SqlParameter object whose type is SqlDbType.Structured and whose value is table. That all works.
The set of column types supported by DataTable appears to be fixed. Most SQL primitive types have their C# equivalent, but there is nothing directly corresponding to the date type supported by MSSQL. We have DateTime in C#, of course, but that is more general. As it happens I have my own simple class MyDate to represent dates. It supports a conversion method ToDateTime().
I would like to teach DataTable about this new type so that I'll be able to do
table.Columns.Add(new DataColumn("DateColumn", typeof(MyDate)));
table.Rows.Add(new MyDate(2018, 10, 11));
Currently, if I try this I get
System.ArgumentException:
The type of column 'DateColumn' is not supported.
The type is 'MyDate'
Is there some interface that MyDate should implement so that DataTable will support it? Ideally, I would like to specify a conversion to DateTime, which obviously is already handled by DataTable. I don't really want to add support for the custom type all the way through the SQL stack down to encoding it on the wire (which is what SqlUserDefinedAggregate seems to be about).
I have tried implementing IConvertible and ISerializable; it's not that. I hoped that the System.Data.SqlTypes namespace would contain an SqlDate type I could use instead of my custom type, but it doesn't.
Obviously I can just use DateTime and convert when adding the values. That is what I have done so far. But the MyDate type is used in a lot of my code base (the convention is to use it for dates, while DateTime is used when we need both date and time), the SQL date type is used in the database, and I'd like to make the code cleaner if I can. Hence I ask, how can I make DataTable support this custom type, or custom types in general?

ObjectListView sorting on date

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

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.

DateTime Insert via LINQ

I have a column in my table which has a DataType of 'timestamp'. Now I am inserting a row through LINQ2SQL. Now what should I write here:
Entity_Product_Point ev = new Entity_Product_Point();
ev.DateCreated = ???
Thanks!
Are you SURE you wanted timestamp? It has nothing to do with dates... If you'd like to store "DateCreated", I think you probably want to use either a DateTime or just Date datatype in MSSQL. If that's what you really intended, then you can pass in DateTime.Now for a value.
Well the corresponding type would be byte[], but this type is used internally by SQL Server for row versioning—and by ORMs for optimistic locking. You should never (can't) write a value out for a timestamp column manually.
For a more complete listing of which type correspond to what, check out this question

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

Categories

Resources