// ////////////////////////////////////////////////////////////////////////
        // PRIVATE METHODS
        //
        /// <summary>
        /// UpdateForReport
        /// </summary>
        private void UpdateForReport()
        {
            ProjectCostingSheetInformationLabourHoursInformationGateway projectCostingSheetInformationLabourHoursInformationGateway = new ProjectCostingSheetInformationLabourHoursInformationGateway(Data);
            projectCostingSheetInformationLabourHoursInformationGateway.ClearBeforeFill = false;

            ProjectCostingSheetInformationUnitsInformationGateway projectCostingSheetInformationUnitsInformationGateway = new ProjectCostingSheetInformationUnitsInformationGateway(Data);
            projectCostingSheetInformationUnitsInformationGateway.ClearBeforeFill = false;

            ProjectCostingSheetInformationMaterialsInformationGateway projectCostingSheetInformationMaterialsInformationGateway = new ProjectCostingSheetInformationMaterialsInformationGateway(Data);
            projectCostingSheetInformationMaterialsInformationGateway.ClearBeforeFill = false;

            ProjectCostingSheetInformationOtherCostsInformationGateway projectCostingSheetInformationOtherCostsInformationGateway = new ProjectCostingSheetInformationOtherCostsInformationGateway(Data);
            projectCostingSheetInformationOtherCostsInformationGateway.ClearBeforeFill = false;

            projectCostingSheetInformationRevenueInformationGateway projectCostingSheetInformationRevenueInformationGateway = new projectCostingSheetInformationRevenueInformationGateway(Data);
            projectCostingSheetInformationRevenueInformationGateway.ClearBeforeFill = false;

            foreach (ProjectCostingSheetInformationTDS.ReportInformationRow row in (ProjectCostingSheetInformationTDS.ReportInformationDataTable)Table)
            {
                projectCostingSheetInformationLabourHoursInformationGateway.LoadByCostingSheetId(row.CostingSheetID, row.COMPANY_ID);
                projectCostingSheetInformationUnitsInformationGateway.LoadByCostingSheetId(row.CostingSheetID, row.COMPANY_ID);
                projectCostingSheetInformationMaterialsInformationGateway.LoadByCostingSheetId(row.CostingSheetID, row.COMPANY_ID);
                projectCostingSheetInformationOtherCostsInformationGateway.LoadByCostingSheetId(row.CostingSheetID, row.COMPANY_ID);
                projectCostingSheetInformationRevenueInformationGateway.LoadByCostingSheetId(row.CostingSheetID, row.COMPANY_ID);
            }
        }
        /// <summary>
        /// UpdateForReport
        /// </summary>
        private void UpdateForReport()
        {
            ProjectCostingSheetInformationLabourHoursInformationGateway projectCostingSheetInformationLabourHoursInformationGateway = new ProjectCostingSheetInformationLabourHoursInformationGateway(Data);
            projectCostingSheetInformationLabourHoursInformationGateway.ClearBeforeFill = false;

            ProjectCostingSheetInformationUnitsInformationGateway projectCostingSheetInformationUnitsInformationGateway = new ProjectCostingSheetInformationUnitsInformationGateway(Data);
            projectCostingSheetInformationUnitsInformationGateway.ClearBeforeFill = false;

            ProjectCostingSheetInformationMaterialsInformationGateway projectCostingSheetInformationMaterialsInformationGateway = new ProjectCostingSheetInformationMaterialsInformationGateway(Data);
            projectCostingSheetInformationMaterialsInformationGateway.ClearBeforeFill = false;

            ProjectCostingSheetInformationSubcontractorsInformationGateway projectCostingSheetInformationSubcontractorsInformationGateway = new ProjectCostingSheetInformationSubcontractorsInformationGateway(Data);
            projectCostingSheetInformationSubcontractorsInformationGateway.ClearBeforeFill = false;

            ProjectCostingSheetInformationOtherCostsInformationGateway projectCostingSheetInformationOtherCostsInformationGateway = new ProjectCostingSheetInformationOtherCostsInformationGateway(Data);
            projectCostingSheetInformationOtherCostsInformationGateway.ClearBeforeFill = false;

            projectCostingSheetInformationRevenueInformationGateway projectCostingSheetInformationRevenueInformationGateway = new projectCostingSheetInformationRevenueInformationGateway(Data);
            projectCostingSheetInformationRevenueInformationGateway.ClearBeforeFill = false;

            ProjectCostingSheetInformationHotelsInformationGateway projectCostingSheetInformationHotelsInformationGateway = new ProjectCostingSheetInformationHotelsInformationGateway(Data);
            projectCostingSheetInformationHotelsInformationGateway.ClearBeforeFill = false;

            ProjectCostingSheetInformationBondingsInformationGateway projectCostingSheetInformationBondingsInformationGateway = new ProjectCostingSheetInformationBondingsInformationGateway(Data);
            projectCostingSheetInformationBondingsInformationGateway.ClearBeforeFill = false;

            ProjectCostingSheetInformationInsurancesInformationGateway projectCostingSheetInformationInsurancesInformationGateway = new ProjectCostingSheetInformationInsurancesInformationGateway(Data);
            projectCostingSheetInformationInsurancesInformationGateway.ClearBeforeFill = false;

            ProjectCostingSheetInformationOtherCategoryInformationGateway projectCostingSheetInformationOtherCategoryInformationGateway = new ProjectCostingSheetInformationOtherCategoryInformationGateway(Data);
            projectCostingSheetInformationOtherCategoryInformationGateway.ClearBeforeFill = false;

            foreach (ProjectCostingSheetInformationTDS.BasicInformationRow row in (ProjectCostingSheetInformationTDS.BasicInformationDataTable)Table)
            {
                if (row.GrandRevenue > 0)
                {
                    row.GrandGrossMargin = (row.GrandProfit / row.GrandRevenue) * 100;
                }
                else
                {
                    row.GrandGrossMargin = 0;
                }

                projectCostingSheetInformationLabourHoursInformationGateway.LoadByCostingSheetId(row.CostingSheetID, row.COMPANY_ID);
                projectCostingSheetInformationUnitsInformationGateway.LoadByCostingSheetId(row.CostingSheetID, row.COMPANY_ID);
                projectCostingSheetInformationMaterialsInformationGateway.LoadByCostingSheetId(row.CostingSheetID, row.COMPANY_ID);
                projectCostingSheetInformationSubcontractorsInformationGateway.LoadByCostingSheetId(row.CostingSheetID, row.COMPANY_ID);
                projectCostingSheetInformationOtherCostsInformationGateway.LoadByCostingSheetId(row.CostingSheetID, row.COMPANY_ID);
                projectCostingSheetInformationRevenueInformationGateway.LoadByCostingSheetId(row.CostingSheetID, row.COMPANY_ID);

                projectCostingSheetInformationHotelsInformationGateway.LoadByCostingSheetId(row.CostingSheetID, row.COMPANY_ID);
                projectCostingSheetInformationBondingsInformationGateway.LoadByCostingSheetId(row.CostingSheetID, row.COMPANY_ID);
                projectCostingSheetInformationInsurancesInformationGateway.LoadByCostingSheetId(row.CostingSheetID, row.COMPANY_ID);
                projectCostingSheetInformationOtherCategoryInformationGateway.LoadByCostingSheetId(row.CostingSheetID, row.COMPANY_ID);
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!Convert.ToBoolean(Session["sgLFS_PROJECTS_COSTINGSHEETS_ADMIN"]))
                {
                    if (!(Convert.ToBoolean(Session["sgLFS_PROJECTS_COSTINGSHEETS_VIEW"]) && Convert.ToBoolean(Session["sgLFS_PROJECTS_COSTINGSHEETS_EDIT"])))
                    {
                        Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                    }
                }

                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) || ((string)Request.QueryString["costing_sheet_id"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in project_costing_sheets_summary.aspx");
                }

                // Tag Page
                hdfCostingSheetId.Value = Request.QueryString["costing_sheet_id"].ToString();
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfProjectId.Value = Request.QueryString["project_id"];
                hdfDataChanged.Value = Request.QueryString["data_changed"];
                hdfDataChangedMessage.Value = "Changes made to this project will not be saved.";

                ViewState["state"] = Request.QueryString["state"];
                ViewState["active_tab"] = Request.QueryString["active_tab"];
                ViewState["origin"] = Request.QueryString["origin"];
                ViewState["update"] = Request.QueryString["update"];

                projectTDS = (ProjectTDS)Session["lfsProjectTDS"];

                Session.Remove("labourHoursInformationDummy");
                Session.Remove("unitsInformationDummy");
                Session.Remove("materialsInformationDummy");
                Session.Remove("otherCostsInformationDummy");
                Session.Remove("revenueInformationDummy");

                Session.Remove("subcontractorsInformationDummy");
                Session.Remove("hotelsInformationDummy");
                Session.Remove("bondingsInformationDummy");
                Session.Remove("insurancesInformationDummy");
                Session.Remove("otherCategoryInformationDummy");

                // If coming from project_costing_sheets_navigator.aspx or project_costing_sheets_add.aspx
                if (Request.QueryString["source_page"] == "project_costing_sheets_navigator.aspx" || Request.QueryString["source_page"] == "project_costing_sheets_add.aspx")
                {
                    // Store Navigator State and Update control
                    StoreNavigatorState();
                    ViewState["update"] = "no";

                    // Get Costing sheet ID
                    int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
                    int costingSheetId = Int32.Parse(hdfCostingSheetId.Value.Trim());

                    // Get dataset
                    projectTDS = (ProjectTDS)Session["lfsProjectTDS"];
                    projectCostingSheetInformationTDS = new ProjectCostingSheetInformationTDS();
                    labourHoursInformation = new ProjectCostingSheetInformationTDS.LabourHoursInformationDataTable();
                    unitsInformation = new ProjectCostingSheetInformationTDS.UnitsInformationDataTable();
                    materialsInformation = new ProjectCostingSheetInformationTDS.MaterialsInformationDataTable();
                    otherCostsInformation = new ProjectCostingSheetInformationTDS.OtherCostsInformationDataTable();
                    revenueInformation = new ProjectCostingSheetInformationTDS.RevenueInformationDataTable();

                    subcontractorsInformation = new ProjectCostingSheetInformationTDS.SubcontractorsInformationDataTable();
                    hotelsInformation = new ProjectCostingSheetInformationTDS.HotelsInformationDataTable();
                    bondingsInformation = new ProjectCostingSheetInformationTDS.BondingsInformationDataTable();
                    insurancesInformation = new ProjectCostingSheetInformationTDS.InsurancesInformationDataTable();
                    otherCategoryInformation = new ProjectCostingSheetInformationTDS.OtherCategoryInformationDataTable();

                    // Get General Data
                    ProjectCostingSheetInformationBasicInformation projectCostingSheetInformationBasicInformation = new ProjectCostingSheetInformationBasicInformation(projectCostingSheetInformationTDS);
                    projectCostingSheetInformationBasicInformation.LoadByCostingSheetId(costingSheetId, companyId);

                    ProjectCostingSheetInformationLabourHoursInformationGateway projectCostingSheetInformationLabourHoursInformationGateway = new ProjectCostingSheetInformationLabourHoursInformationGateway(projectCostingSheetInformationTDS);
                    projectCostingSheetInformationLabourHoursInformationGateway.LoadByCostingSheetId(costingSheetId, companyId);

                    ProjectCostingSheetInformationUnitsInformationGateway projectCostingSheetInformationUnitsInformationGateway = new ProjectCostingSheetInformationUnitsInformationGateway(projectCostingSheetInformationTDS);
                    projectCostingSheetInformationUnitsInformationGateway.LoadByCostingSheetId(costingSheetId, companyId);

                    ProjectCostingSheetInformationMaterialsInformationGateway projectCostingSheetInformationMaterialsInformationGateway = new ProjectCostingSheetInformationMaterialsInformationGateway(projectCostingSheetInformationTDS);
                    projectCostingSheetInformationMaterialsInformationGateway.LoadByCostingSheetId(costingSheetId, companyId);

                    ProjectCostingSheetInformationOtherCostsInformationGateway projectCostingSheetInformationOtherCostsInformationGateway = new ProjectCostingSheetInformationOtherCostsInformationGateway(projectCostingSheetInformationTDS);
                    projectCostingSheetInformationOtherCostsInformationGateway.LoadByCostingSheetId(costingSheetId, companyId);

                    projectCostingSheetInformationRevenueInformationGateway projectCostingSheetInformationRevenueInformationGateway = new projectCostingSheetInformationRevenueInformationGateway(projectCostingSheetInformationTDS);
                    projectCostingSheetInformationRevenueInformationGateway.LoadByCostingSheetId(costingSheetId, companyId);

                    ProjectCostingSheetInformationSubcontractorsInformationGateway projectCostingSheetInformationSubcontractorsInformationGateway = new ProjectCostingSheetInformationSubcontractorsInformationGateway(projectCostingSheetInformationTDS);
                    projectCostingSheetInformationSubcontractorsInformationGateway.LoadByCostingSheetId(costingSheetId, companyId);

                    ProjectCostingSheetInformationHotelsInformationGateway projectCostingSheetInformationHotelsInformationGateway = new ProjectCostingSheetInformationHotelsInformationGateway(projectCostingSheetInformationTDS);
                    projectCostingSheetInformationHotelsInformationGateway.LoadByCostingSheetId(costingSheetId, companyId);

                    ProjectCostingSheetInformationBondingsInformationGateway projectCostingSheetInformationBondingsInformationGateway = new ProjectCostingSheetInformationBondingsInformationGateway(projectCostingSheetInformationTDS);
                    projectCostingSheetInformationBondingsInformationGateway.LoadByCostingSheetId(costingSheetId, companyId);

                    ProjectCostingSheetInformationInsurancesInformationGateway projectCostingSheetInformationInsurancesInformationGateway = new ProjectCostingSheetInformationInsurancesInformationGateway(projectCostingSheetInformationTDS);
                    projectCostingSheetInformationInsurancesInformationGateway.LoadByCostingSheetId(costingSheetId, companyId);

                    ProjectCostingSheetInformationOtherCategoryInformationGateway projectCostingSheetInformationOtherCategoryInformationGateway = new ProjectCostingSheetInformationOtherCategoryInformationGateway(projectCostingSheetInformationTDS);
                    projectCostingSheetInformationOtherCategoryInformationGateway.LoadByCostingSheetId(costingSheetId, companyId);

                    Session["lfsProjectTDS"] = projectTDS;
                    Session["projectCostingSheetInformationTDS"] = projectCostingSheetInformationTDS;
                    Session["labourHoursInformation"] = projectCostingSheetInformationTDS.LabourHoursInformation;
                    Session["unitsInformation"] = projectCostingSheetInformationTDS.UnitsInformation;
                    Session["materialsInformation"] = projectCostingSheetInformationTDS.MaterialsInformation;
                    Session["otherCostsInformation"] = projectCostingSheetInformationTDS.OtherCostsInformation;
                    Session["revenueInformation"] = projectCostingSheetInformationTDS.RevenueInformation;

                    Session["subcontractorsInformation"] = projectCostingSheetInformationTDS.SubcontractorsInformation;
                    Session["hotelsInformation"] = projectCostingSheetInformationTDS.HotelsInformation;
                    Session["bondingsInformation"] = projectCostingSheetInformationTDS.BondingsInformation;
                    Session["insurancesInformation"] = projectCostingSheetInformationTDS.InsurancesInformation;
                    Session["otherCategoryInformation"] = projectCostingSheetInformationTDS.OtherCategoryInformation;

                    labourHoursInformation = projectCostingSheetInformationTDS.LabourHoursInformation;
                    unitsInformation = projectCostingSheetInformationTDS.UnitsInformation;
                    materialsInformation = projectCostingSheetInformationTDS.MaterialsInformation;
                    otherCostsInformation = projectCostingSheetInformationTDS.OtherCostsInformation;
                    revenueInformation = projectCostingSheetInformationTDS.RevenueInformation;

                    subcontractorsInformation = projectCostingSheetInformationTDS.SubcontractorsInformation;
                    hotelsInformation = projectCostingSheetInformationTDS.HotelsInformation;
                    bondingsInformation = projectCostingSheetInformationTDS.BondingsInformation;
                    insurancesInformation = projectCostingSheetInformationTDS.InsurancesInformation;
                    otherCategoryInformation = projectCostingSheetInformationTDS.OtherCategoryInformation;
                }

                // ... project_costing_sheets_add.aspx
                if (Request.QueryString["source_page"] == "project_costing_sheets_add.aspx")
                {
                    ViewState["update"] = "yes";
                }

                // ... left menu, project_costing_sheets_edit.aspx, project_costing_sheets_delete.aspx or project_costing_sheets_state.aspx
                if ((Request.QueryString["source_page"] == "lm") || (Request.QueryString["source_page"] == "project_costing_sheets_edit.aspx") || (Request.QueryString["source_page"] == "project_costing_sheets_delete.aspx") || (Request.QueryString["source_page"] == "project_costing_sheets_state.aspx"))
                {
                    // Store Navigator State and Update control
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];
                }

                // Restore dataset
                projectTDS = (ProjectTDS)Session["lfsProjectTDS"];
                projectCostingSheetInformationTDS = (ProjectCostingSheetInformationTDS)Session["projectCostingSheetInformationTDS"];

                labourHoursInformation = (ProjectCostingSheetInformationTDS.LabourHoursInformationDataTable)Session["labourHoursInformation"];
                unitsInformation = (ProjectCostingSheetInformationTDS.UnitsInformationDataTable)Session["unitsInformation"];
                materialsInformation = (ProjectCostingSheetInformationTDS.MaterialsInformationDataTable)Session["materialsInformation"];
                otherCostsInformation = (ProjectCostingSheetInformationTDS.OtherCostsInformationDataTable)Session["otherCostsInformation"];
                revenueInformation = (ProjectCostingSheetInformationTDS.RevenueInformationDataTable)Session["revenueInformation"];

                subcontractorsInformation = (ProjectCostingSheetInformationTDS.SubcontractorsInformationDataTable)Session["subcontractorsInformation"];
                hotelsInformation = (ProjectCostingSheetInformationTDS.HotelsInformationDataTable)Session["hotelsInformation"];
                bondingsInformation = (ProjectCostingSheetInformationTDS.BondingsInformationDataTable)Session["bondingsInformation"];
                insurancesInformation = (ProjectCostingSheetInformationTDS.InsurancesInformationDataTable)Session["insurancesInformation"];
                otherCategoryInformation = (ProjectCostingSheetInformationTDS.OtherCategoryInformationDataTable)Session["otherCategoryInformation"];

                ProjectGateway projectGateway = new ProjectGateway(projectTDS);
                hdfClientId.Value = projectGateway.GetClientID(Int32.Parse(hdfProjectId.Value.ToString())).ToString();

                // ... for project
                int currentProjectId = Int32.Parse(hdfProjectId.Value.ToString());
                string name = projectGateway.GetName(currentProjectId);
                if (name.Length > 23) name = name.Substring(0, 20) + "...";
                lblTitleProjectName.Text = name + " (" + projectGateway.GetProjectNumber(currentProjectId) + ")";

                // ... for client
                int currentClientId = projectGateway.GetClientID(Int32.Parse(hdfProjectId.Value.ToString()));
                CompaniesGateway companiesGateway = new CompaniesGateway();
                companiesGateway.LoadAllByCompaniesId(currentClientId, Int32.Parse(hdfCompanyId.Value));
                lblTitleClientName.Text = "Client: " + companiesGateway.GetName(currentClientId);

                LoadBasicData();
            }
            else
            {
                // Restore dataset
                projectTDS = (ProjectTDS)Session["lfsProjectTDS"];
                projectCostingSheetInformationTDS = (ProjectCostingSheetInformationTDS)Session["projectCostingSheetInformationTDS"];

                labourHoursInformation = (ProjectCostingSheetInformationTDS.LabourHoursInformationDataTable)Session["labourHoursInformation"];
                unitsInformation = (ProjectCostingSheetInformationTDS.UnitsInformationDataTable)Session["unitsInformation"];
                materialsInformation = (ProjectCostingSheetInformationTDS.MaterialsInformationDataTable)Session["materialsInformation"];
                otherCostsInformation = (ProjectCostingSheetInformationTDS.OtherCostsInformationDataTable)Session["otherCostsInformation"];
                revenueInformation = (ProjectCostingSheetInformationTDS.RevenueInformationDataTable)Session["revenueInformation"];

                subcontractorsInformation = (ProjectCostingSheetInformationTDS.SubcontractorsInformationDataTable)Session["subcontractorsInformation"];
                hotelsInformation = (ProjectCostingSheetInformationTDS.HotelsInformationDataTable)Session["hotelsInformation"];
                bondingsInformation = (ProjectCostingSheetInformationTDS.BondingsInformationDataTable)Session["bondingsInformation"];
                insurancesInformation = (ProjectCostingSheetInformationTDS.InsurancesInformationDataTable)Session["insurancesInformation"];
                otherCategoryInformation = (ProjectCostingSheetInformationTDS.OtherCategoryInformationDataTable)Session["otherCategoryInformation"];
            }
        }
        /// <summary>
        /// Save all Materials Costing Sheets
        /// </summary>
        /// <param name="companyId">companyId</param>
        /// <param name="costingSheetId">costingSheetId</param>
        public void Save(int companyId, int costingSheetId)
        {
            ProjectCostingSheetInformationTDS materialsInformationChanges = (ProjectCostingSheetInformationTDS)Data.GetChanges();

            if (materialsInformationChanges.MaterialsInformation.Rows.Count > 0)
            {
                ProjectCostingSheetInformationMaterialsInformationGateway projectCostingSheetInformationMaterialsInformationGateway = new ProjectCostingSheetInformationMaterialsInformationGateway(materialsInformationChanges);

                foreach (ProjectCostingSheetInformationTDS.MaterialsInformationRow row in (ProjectCostingSheetInformationTDS.MaterialsInformationDataTable)materialsInformationChanges.MaterialsInformation)
                {
                    // Insert new costing sheet Materials
                    if ((!row.Deleted) && (!row.InDatabase))
                    {
                        ProjectCostingSheetMaterials materials = new ProjectCostingSheetMaterials(null);
                        materials.InsertDirect(costingSheetId, row.MaterialID, row.RefID, row.UnitOfMeasurement, row.Quantity, row.CostCad, row.TotalCostCad, row.CostUsd, row.TotalCostUsd, row.Deleted, row.COMPANY_ID, row.StartDate, row.EndDate, row.Function_);
                    }

                    // Update costing sheet Materials
                    if ((!row.Deleted) && (row.InDatabase))
                    {
                        int materialId = row.MaterialID;
                        int refId = row.RefID;
                        bool deleted = false;

                        //original values
                        string originalUnitOfMeasurement = projectCostingSheetInformationMaterialsInformationGateway.GetUnitOfMeasurementOriginal(costingSheetId, materialId, refId);
                        double originalQuantity = projectCostingSheetInformationMaterialsInformationGateway.GetQuantityOriginal(costingSheetId, materialId, refId);
                        decimal originalCostCad = projectCostingSheetInformationMaterialsInformationGateway.GetCostCadOriginal(costingSheetId, materialId, refId);
                        decimal originalTotalCostCad = projectCostingSheetInformationMaterialsInformationGateway.GetTotalCostCadOriginal(costingSheetId, materialId, refId);
                        decimal originalCostUsd = projectCostingSheetInformationMaterialsInformationGateway.GetCostUsdOriginal(costingSheetId, materialId, refId);
                        decimal originalTotalCostUsd = projectCostingSheetInformationMaterialsInformationGateway.GetTotalCostUsdOriginal(costingSheetId, materialId, refId);
                        DateTime originalStartDate = projectCostingSheetInformationMaterialsInformationGateway.GetStartDateOriginal(costingSheetId, materialId, refId);
                        DateTime originalEndDate = projectCostingSheetInformationMaterialsInformationGateway.GetEndDateOriginal(costingSheetId, materialId, refId);
                        string originalFunction_ = projectCostingSheetInformationMaterialsInformationGateway.GetFunction_Original(costingSheetId, materialId, refId);

                        //original values
                        string newUnitOfMeasurement = projectCostingSheetInformationMaterialsInformationGateway.GetUnitOfMeasurement(costingSheetId, materialId, refId);
                        double newQuantity = projectCostingSheetInformationMaterialsInformationGateway.GetQuantity(costingSheetId, materialId, refId);
                        decimal newCostCad = projectCostingSheetInformationMaterialsInformationGateway.GetCostCad(costingSheetId, materialId, refId);
                        decimal newTotalCostCad = projectCostingSheetInformationMaterialsInformationGateway.GetTotalCostCad(costingSheetId, materialId, refId);
                        decimal newCostUsd = projectCostingSheetInformationMaterialsInformationGateway.GetCostUsd(costingSheetId, materialId, refId);
                        decimal newTotalCostUsd = projectCostingSheetInformationMaterialsInformationGateway.GetTotalCostUsd(costingSheetId, materialId, refId);
                        DateTime newStartDate = projectCostingSheetInformationMaterialsInformationGateway.GetStartDate(costingSheetId, materialId, refId);
                        DateTime newEndDate = projectCostingSheetInformationMaterialsInformationGateway.GetEndDate(costingSheetId, materialId, refId);
                        string newFunction_ = projectCostingSheetInformationMaterialsInformationGateway.GetFunction_(costingSheetId, materialId, refId);

                        ProjectCostingSheetMaterials materials = new ProjectCostingSheetMaterials(null);
                        materials.UpdateDirect(costingSheetId, materialId, refId, originalUnitOfMeasurement, originalQuantity, originalCostCad, originalTotalCostCad, originalCostUsd, originalTotalCostUsd, deleted, companyId, originalStartDate, originalEndDate, originalFunction_, newUnitOfMeasurement, newQuantity, newCostCad, newTotalCostCad, newCostUsd, newTotalCostUsd, deleted, companyId, newStartDate, newEndDate, newFunction_);
                    }

                    // Delete costing sheet Materials
                    if ((row.Deleted) && (row.InDatabase))
                    {
                        ProjectCostingSheetMaterials materials = new ProjectCostingSheetMaterials(null);
                        materials.DeleteDirect(costingSheetId, row.MaterialID, row.RefID, row.COMPANY_ID);
                    }
                }
            }
        }
        protected void grdMaterials_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // Edit items
            if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState == DataControlRowState.Edit) || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate))))
            {
                int costingSheetId = Int32.Parse(((Label)e.Row.FindControl("lblCostingSheetIDEdit")).Text.Trim());
                int materialId = Int32.Parse(((Label)e.Row.FindControl("lblMaterialIDEdit")).Text.Trim());
                int refId = Int32.Parse(((Label)e.Row.FindControl("lblRefIDEdit")).Text.Trim());

                ProjectCostingSheetInformationMaterialsInformationGateway projectCostingSheetInformationMaterialsInformationGateway = new ProjectCostingSheetInformationMaterialsInformationGateway(projectCostingSheetInformationTDS);
                string unitOfMeasurement = projectCostingSheetInformationMaterialsInformationGateway.GetUnitOfMeasurement(costingSheetId, materialId, refId);
                ((DropDownList)e.Row.FindControl("ddlUnitOfMeasurementMaterialsEdit")).SelectedValue = unitOfMeasurement;

                bool inDatabase = projectCostingSheetInformationMaterialsInformationGateway.GetInDatabase(costingSheetId, materialId, refId);

                if (inDatabase)
                {
                    ((RadDatePicker)e.Row.FindControl("tkrdpEndDateEdit")).Calendar.Enabled = false;
                    ((RadDatePicker)e.Row.FindControl("tkrdpEndDateEdit")).DateInput.ReadOnly = true;

                    ((RadDatePicker)e.Row.FindControl("tkrdpStartDateEdit")).Calendar.Enabled = false;
                    ((RadDatePicker)e.Row.FindControl("tkrdpStartDateEdit")).DateInput.ReadOnly = true;
                }

                ((RadDatePicker)e.Row.FindControl("hdfFrom")).SelectedDate = Convert.ToDateTime(hdfFromDate.Value);
                ((RadDatePicker)e.Row.FindControl("hdfTo")).SelectedDate = Convert.ToDateTime(hdfToDate.Value);
            }

            // Footer Item
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                ((RadDatePicker)e.Row.FindControl("hdfFrom")).SelectedDate = Convert.ToDateTime(hdfFromDate.Value);
                ((RadDatePicker)e.Row.FindControl("hdfTo")).SelectedDate = Convert.ToDateTime(hdfToDate.Value);
            }
        }