Click here to Skip to main content
15,903,741 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I have a datagrid view, a use datatable to populate it.

make some math calculation, but i whant to include in my calculation a difference between dates, in this line of code:

Table.Columns.Add(new DataColumn("DePlataAzi", typeof(Decimal), "((Total_Platit * (Procent/100)) * 5) + Total_Platit"));


I whant to replace the "5" with the value of diffence betwenn to dates.

If posible in the difference between dates i whant take into consideration the current day. For example today is 27.03.2011, next day 28.03.2011 result 2 day.

thanks.



This is the al code:
dataContracteClienti.Rows.Clear();
            Program.Connection.CommandText = "select Contracts.ContractId, Contracts.StartDate, Contracts.EndDate, Contracts.Procent, sum(ContractItems.Payment) AS Total_Platit from Contracts INNER JOIN ContractItems ON Contracts.ContractId = ContractItems.ContractId WHERE ClientID=@ClientID "
                + "GROUP BY Contracts.ContractId, Contracts.StartDate, Contracts.EndDate, Contracts.Procent ORDER BY Contracts.StartDate";
            Program.Connection.AddParameter("@ClientID", cboNumeClient.SelectedValue.ToString());                    
            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);
       

            Table.Columns.Add(new DataColumn("DePlataAzi", typeof(Decimal), "((Total_Platit * (Procent/100)) * 5) + Total_Platit"));
            Table.Columns.Add(new DataColumn("DePlataLaTermen", typeof(Decimal), "((Total_Platit * (Procent/100)) * 20) + Total_Platit"));
            

            foreach (DataRow Row in Table.Rows)
            {
                dataContracteClienti.Rows.Add(Row["ContractId"].ToString(), Convert.ToDateTime(Row["StartDate"].ToString()), Convert.ToDateTime(Row["EndDate"].ToString()), Convert.ToInt32(Row["Total_Platit"].ToString()), Convert.ToDecimal(Row["DePlataAzi"].ToString()), Convert.ToDecimal(Row["DePlataLaTermen"].ToString()));
            }


UPDATE:
Resolved by OP and posted as an answer.
Posted
Updated 27-Mar-11 9:26am
v4

If you want to fetch the difference in the SQL statement, you can use DATEDIFF[^]. For example the following gets the difference between today and tomorrow in days:
SQL
SELECT DATEDIFF(day, GETDATE(), DATEADD(day, 1, GETDATE()))
 
Share this answer
 
Comments
aciobanita 27-Mar-11 13:52pm    
i try this


Program.Connection.CommandText = "select Contracts.ContractId, Contracts.StartDate, Contracts.EndDate, Contracts.Procent, sum(ContractItems.Payment) AS Total_Platit, DATEDIFF (day, Contracts.StartDate, Contracts.EndDate) AS ShippingDays from Contracts INNER JOIN ContractItems ON Contracts.ContractId = ContractItems.ContractId WHERE ClientID=@ClientID "
+ "GROUP BY Contracts.ContractId, Contracts.StartDate, Contracts.EndDate, Contracts.Procent ORDER BY Contracts.StartDate";

error: No value given for one or more required parameters.
Wendelius 27-Mar-11 13:59pm    
The error is something different. Most likely you haven't given a value for @ClientID or the SqlDbType is wrong. Debug the portion where you add the parameter to the command to see if it's correctly.
aciobanita 27-Mar-11 14:05pm    
if i remouve the DATEDIFF (day, Contracts.StartDate, Contracts.EndDate) AS ShippingDays, no error
Wendelius 27-Mar-11 14:12pm    
That's weird, tested with several variations and always working fine. Can you take the statement to Sql Server Management Studio and test the same statement there?
Costica U 27-Mar-11 14:33pm    
Data type for Contracts.StartDate and Contracts.EndDate must be DATETIME.
If not use DATEDIFF (day, CONVERT(DATETIME,Contracts.StartDate), CONVERT(DATETIME,Contracts.EndDate)) AS ShippingDays
You can either do this at the database end as Mika suggested or you can do it manually in the TableNewRow or RowChanged event as appropriate. In the event handler, manually do the difference and then add one to include one of the dates in the result.
 
Share this answer
 
I rezolved, i create a litle class

C#
using System;
using System.Collections.Generic;
using System.Text;
namespace CalculareDiferenta
{
    public class DiferentaData
    {
        private int Zi;
        public DiferentaData(DateTime d1, DateTime d2)
        {
            TimeSpan ts = d1.Subtract(d2);
            Zi = ts.Days;

        }
        public override string ToString()
        {
            return this.Zi.ToString();
        }
        public int Zile
        {
            get
            {
                return this.Zi;
            }
        }
    }
}


and the finall cod look like this

dataContracteClienti.Rows.Clear();
            Program.Connection.CommandText = "select Contracts.ContractId, Contracts.StartDate, Contracts.EndDate, Contracts.Procent, sum(ContractItems.Payment) AS Total_Platit from Contracts INNER JOIN ContractItems ON Contracts.ContractId = ContractItems.ContractId WHERE ClientID=@ClientID "
                + "GROUP BY Contracts.ContractId, Contracts.StartDate, Contracts.EndDate, Contracts.Procent ORDER BY Contracts.StartDate";
            Program.Connection.AddParameter("@ClientID", cboNumeClient.SelectedValue.ToString());                    
            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);

            DiferentaData DiferentaDePlataLaTermen = new DiferentaData(Convert.ToDateTime(Table.Rows[0]["EndDate"].ToString()), Convert.ToDateTime(Table.Rows[0]["StartDate"].ToString()));
            DiferentaData DiferentaDePlataAzi = new DiferentaData(Convert.ToDateTime(DateTime.Now.ToShortDateString()), Convert.ToDateTime(Table.Rows[0]["StartDate"].ToString()));

            Table.Columns.Add(new DataColumn("DePlataLaTermen", typeof(Decimal), "((Total_Platit * (Procent/100)) * (" + DiferentaDePlataLaTermen.ToString() + "+ 1 )) + Total_Platit"));
            Table.Columns.Add(new DataColumn("DePlataAzi", typeof(Decimal), "((Total_Platit * (Procent/100)) * (" + DiferentaDePlataAzi + " +1 )) + Total_Platit"));

            foreach (DataRow Row in Table.Rows)
            {
                
                dataContracteClienti.Rows.Add(Row["ContractId"].ToString(), Convert.ToDateTime(Row["StartDate"].ToString()), Convert.ToDateTime(Row["EndDate"].ToString()), Convert.ToInt32(Row["Total_Platit"].ToString()), Convert.ToDecimal(Row["DePlataAzi"].ToString()), Convert.ToDecimal(Row["DePlataLaTermen"].ToString()));
            }



I hope in the future to work ok, i'm new in C#,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900