|
|
DataColumn.Expression to calc Date difference |
|
Author |
Message |
Vincent 0
|
Posted: Visual C# Express Edition, DataColumn.Expression to calc Date difference |
Top |
Hello,
I have two DateTime columns in a table, and I want to calculate the day difference between the two, and display it in another column.
I add a DataColumn in Dataset, and write an equation in Expression property as below:
Convert(RequiredDate,'System.Double')-Convert(OrderDate,'System.Double')
But it does not work, I have also searched help in MSDN:
http://www.hide-link.com/ (VS.80).aspx
However, I can't find anything helpful.
Anyone give an advice thanks!
Visual Studio Express Editions2
|
|
|
|
|
Yogesh Prabhu
|
Posted: Visual C# Express Edition, DataColumn.Expression to calc Date difference |
Top |
It might be easier if you do the date difference in the backend database. If you are using SQL Server at the backend, you can use DATEDIFF function:
SELECT
OrderID, OrderDate, RequiredDate,
DATEDIFF(d, OrderDate, RequiredDate) AS Diff
FROM
Orders
You can they display the Diff column just like any other column in you grid.
|
|
|
|
|
Vincent 0
|
Posted: Visual C# Express Edition, DataColumn.Expression to calc Date difference |
Top |
Yogesh,
Appreciate your reply for giving a good advice, from the view point of SQL command, really solve this problem. And that also prevent me from getting into a dead end.
After testing this function, the new Diff column can show the days difference correctly. That's great! However it can't recalculate the difference value immediately when I modify OrderDate or RequiredDate on the grid. Unless I select table again.
So I add a grid event - CellEndEdit to calculate the value when I have edit these two columns.
Thanks
|
|
|
|
|
lakman
|
Posted: Visual C# Express Edition, DataColumn.Expression to calc Date difference |
Top |
I am in a similar requirement, but cant use the function in the stored proc itself. My date values come from different databases, hence I must try and use the DATEDIFF as a column expression. Any alternative for such situation thanks
lax.
|
|
|
|
|
Cesar.NET
|
Posted: Visual C# Express Edition, DataColumn.Expression to calc Date difference |
Top |
you can try this:
private void addColumnDiff() { dsSeguimiento.Tables[1].Columns.Add( "Transcurrido", Type.GetType("System.Int32")); dsSeguimiento.Tables[1].RowChanged += new DataRowChangeEventHandler(dtActividad_Changed); }
private void dtActividad_Changed(object sender, System.Data.DataRowChangeEventArgs e)
{
if (e.Row["FechaInicio"] != DBNull.Value && e.Row["FechaFin"] != DBNull.Value) {
//Sacamos los dias de atraso TimeSpan ts = (((DateTime)e.Row["FechaInicio"]).Date - ((DateTime)e.Row["FechaFin"]).Date);
//La condicion es importante para que no se cicle el la funcion if((int)(e.Row["Transcurrido"]!=DBNull.Value e.Row["Transcurrido"]:0) != ts.Days) e.Row["Transcurrido"] = ts.Days; }
|
|
|
|
|
|
|