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.
Related
I have a C# program that reads data from a CSV file into a DataTable. This means that all the columns in the resultant DataTable are strings. I then need to Select from the DataTable a subset of rows that match the criteria of a shared value within a single column. This works fine.
My problem comes in that I also need the return set sorted by another column which is a date. I cannot figure out how to format that sort parameter in the Select function to convert the string expression of the date to a real date so the sort works properly.
Can this be done with the Select statement or do I need to take another approach?
You can load file using schema.ini https://msdn.microsoft.com/en-us/library/ms709353.aspx and define type for your columns
Here is one thing you can try. This will iterate over each row in the DataTable and get the String of the date from the column that you specify. It will turn the String into a DateTime using TryParse. If this succeeds, Row[0] will be set to the DateTime. If it fails, row[0] will not be changed.
I have set the column to 0 in the below code, but you should change all 0's to whatever column the DateTime is in.
DateTime dateValue;
foreach(DataRow row in dtDataTable.Rows){
if(DateTime.TryParse(row[0], out dateValue)){
row[0] = dateValue
}
}
You can find more info on TryParse here. TryParse can have unpredictable behavior, so for even more control I would recommend upgrading to TryParseExact once you get this working.
I need to get the exact type of a column in a DataTable in C#.
In a simliar article on SO i found part of a solution:
DataTable st = reader.GetSchemaTable();
foreach (DataRow row in st.Rows)
{
Console.Write(string.Format("ColumnName:{0} DataType:{1} Ordinal:{2} Precision:{3} Size:{4} Scale:{5}",
row["ColumnName"], row["DataTypeName"], row["ColumnOrdinal"],
row["NumericPrecision"], row["ColumnSize"], row["NumericScale"]));
}
This works for string and date so far.
But for decimal i always recieve nullfor precision and scale.
I need those values to create a new table in another database.
The DataType returned is Decimal.
In the MSDN documentation it says that NumericPrecision returns null if it is no numeric data type, but decimal is kind of numeric?
So how do i get the exact precision of decimal values from the DataSet? Or rather what am I doing wrong?
edit:
I am trying to create some new SQL database based on old .dbf files from a FoxPro application.
So i want to read the columns, and create them in SQL, therefore I need the exact type of the column.
I tried with the .dbf and a new sql database.
I created a table in both to know the exact type for sure, for both databases i get those null returns. There is only a problem with the decimal/numeric values, for string and DateTime i get the Size.
For the .dbf I use a OleDbConnection.
For the SQL I use a SqlConnection, I get null if I try it for each.
From my experience with MySQL, the decimal datatype is numeric, but it can hold any precision or scale. If the database you are trying to create doesn't have the decimal datatype, you may need to convert it to a different type of number and possibly guess what precision and scale you need.
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.
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
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.