/// <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);
            }
        }