private void loadCompleted(object sender, EventArgs e)
        {
            //wait till fully loaded
            if (ctx.IsLoading) return;

            var report = ((LoadOperation<FilmConsumptionReportRow>)sender).Entities;

            //Can't directly bind the report to the grid due to variable no of columns. Using a custom datagrid bindable
            //to a datatable instead

            reportTable = new DataTable("Report");
            var cols = reportTable.Columns;
            var rows = reportTable.Rows;

            var headerRow = new DataRow();
            rows.Add(headerRow);

            var subHeaderRow = new DataRow();
            rows.Add(subHeaderRow);

            AddTextColumn(reportTable, "ReportNo", "Report No");
            AddTextColumn(reportTable, "ReportDate", "Report Date");
            AddTextColumn(reportTable, "FPNo", "FP No");
            AddTextColumn(reportTable, "RTNo", "RT No");

            headerRow["ReportNo"] = "Report No";
            headerRow["ReportDate"] = "Date";
            headerRow["FPNo"] = "FP No";
            headerRow["RTNo"] = "RT No";

            subHeaderRow["ReportNo"] = "";
            subHeaderRow["ReportDate"] = "";
            subHeaderRow["FPNo"] = "";
            subHeaderRow["RTNo"] = "";

            //filmsize columns
            foreach (var row in ctx.Energies)
            {
                foreach (var type in rowTypes)
                {
                    var colName = String.Format("{0}{1}", row.Name, type);
                    AddTextColumn(reportTable, colName, colName);
                    headerRow[colName] = type == "ACCEPTABLE" ? row.Name : string.Empty;
                    subHeaderRow[colName] = type == "ACCEPTABLE" ? "T" : type == "REPAIR" ? "RP" : type == "RETAKE" ? "RT" : "RS";
                }
            }

            string prevReportNo = "";

            DataRow dataRow = null;

            float totalArea = 0;

            string prevEnergy = String.Empty;
            int rowCount = 1;

            //the loop pivots the data wrt to energy and row types
            foreach (var r in report)
            {
                if(r.ReportNo != prevReportNo)
                {
                    if (dataRow != null)
                    {
                        //prev Row last energy total col - temp fix for issue 0000109
                        dataRow[prevEnergy + "ACCEPTABLE"] = totalArea;
                        totalArea = 0;
                        rows.Add(dataRow);
                    }
                    dataRow = new DataRow();
                    dataRow["ReportNo"] = prevReportNo = r.ReportNo; //set prevReportNo for next time
                    dataRow["ReportDate"] = r.Date;
                    dataRow["FPNo"] = r.FPNo;
                    dataRow["RTNo"] = r.RTNo;
                }

                //prev Row last energy total col - temp fix for issue 0000109
                if (r.Energy != prevEnergy)
                {
                    dataRow[prevEnergy + "ACCEPTABLE"] = totalArea;
                    totalArea = 0;
                }

                dataRow[r.Energy + r.RowType] = r.Area;
                totalArea += r.Area;

                prevEnergy = r.Energy;
            }
            //last report row
            if(dataRow != null) rows.Add(dataRow);

            //totals row
            var totalRow = new DataRow();

            foreach (var row in ctx.Energies)
            {
                foreach (var type in rowTypes)
                {
                    var colName = String.Format("{0}{1}", row.Name, type);
                    totalRow[colName] = rows.Select(p => (p[colName] as float?) ?? 0).Sum();
                }
            }

            rows.Add(totalRow);

            var ds = new DataSet("ReportDataSet");
            ds.Tables.Add(reportTable);

            reportGrid.DataSource = ds;
            reportGrid.DataMember = "Report";
            reportGrid.DataBind();

            busyIndicator.IsBusy = false;
        }
        /// <summary>
        /// Sample method to load random data into the grid
        /// </summary>
        public void LoadData()
        {
            // Create some sample names
            List<string> firstNames = new List<string>() { "Peter", "Frank", "Joe", "Lewis", "Jack", "Andrew", "Susan", "Marie", "Linda", "Anne", "Claire", "Debra" };
            List<string> lastNames = new List<string>() { "Smith", "Brown", "Green", "Parker", "Johnson", "Jackson", "Ford", "Sullivan" };
            List<string> icons = new List<string>() { "UserGreen.png", "UserYellow.png", "UserRed.png" };

            // Create a sample DataTable
            DataTable dt = new DataTable("MyDataTable");

            // Create a column
            DataColumn dc1 = new DataColumn("col1");
            dc1.Caption = "First Name";
            dc1.ReadOnly = true;
            dc1.DataType = typeof(String);
            dc1.AllowResize = true;
            dc1.AllowSort = true;
            dc1.AllowReorder = true;
            dt.Columns.Add(dc1);

            // Create a column
            DataColumn dc2 = new DataColumn("col2");
            dc2.Caption = "Last Name";
            dc2.ReadOnly = true;
            dc2.DataType = typeof(String);
            dc2.AllowResize = true;
            dc2.AllowSort = true;
            dc2.AllowReorder = true;
            dt.Columns.Add(dc2);

            // Create a column
            DataColumn dc3 = new DataColumn("col3");
            dc3.Caption = "Age";
            dc3.ReadOnly = false;
            dc3.DataType = typeof(Int32);
            dt.Columns.Add(dc3);

            // Create a column
            DataColumn dc4 = new DataColumn("col4");
            dc4.Caption = "Married";
            dc4.ReadOnly = true;
            dc4.DataType = typeof(Boolean);
            dt.Columns.Add(dc4);

            // Create a column
            DataColumn dc5 = new DataColumn("col5");
            dc5.Caption = "Membership expiration";
            dc5.ReadOnly = true;
            dc5.DataType = typeof(DateTime);
            dt.Columns.Add(dc5);

            // Create a column
            DataColumn dc6 = new DataColumn("col6", typeof(Image));
            dc6.Caption = "Status";
            dc6.ReadOnly = true;
            dt.Columns.Add(dc6);

            // Add sample rows to the table
            Random r = new Random();
            for (int i = 0; i < 15; i++)
            {
                DataRow dr = new DataRow();
                dr["col1"] = firstNames[r.Next(firstNames.Count)];
                dr["col2"] = lastNames[r.Next(lastNames.Count)];
                dr["col3"] = r.Next(20, 81);
                dr["col4"] = (r.Next(0,2) == 1);
                dr["col5"] = DateTime.Now.AddDays(r.Next(10));

                Uri uri = new Uri("Images/" + icons[r.Next(icons.Count)], UriKind.RelativeOrAbsolute);
                ImageSource imgSource = new BitmapImage(uri);
                Image img = new Image();
                img.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(img_ImageFailed);
                img.Source = new BitmapImage(uri);

                dr["col6"] = img;
                dt.Rows.Add(dr);
            }

            // Create a DataSet and add the table to it
            DataSet ds = new DataSet("MyDataSet");
            ds.Tables.Add(dt);

            // Do the binding
            myBindableDG.AutoGenerateColumns = false;
            myBindableDG.DataSource = ds;
            myBindableDG.DataMember = "MyDataTable";
            myBindableDG.DataBind();

            // We could do this as well
            // myBindableDG.DataSource = dt;
            // myBindableDG.DataBind();
        }
        private void loadCompleted(object sender, EventArgs e)
        {
            var report = ((LoadOperation<FixedPatternPerformanceRow>)sender).Entities;

            //Can't directly bind the report to the grid due to variable no of columns. Using a custom datagrid bindable
            //to a datatable instead

            reportTable = new DataTable("Report");

            var cols = reportTable.Columns;
            var rows = reportTable.Rows;

            var locationRow = new DataRow();
            rows.Add(locationRow);

            var segmentRow = new DataRow();
            rows.Add(segmentRow);

            AddTextColumn(reportTable, "FPNo", "FP No");
            AddTextColumn(reportTable, "RTNo", "RT No");
            AddTextColumn(reportTable, "ReportNo", "Report No");
            AddTextColumn(reportTable, "Coverage", "Coverage");
            AddTextColumn(reportTable, "HeatNo", "Heat No");
            AddTextColumn(reportTable, "Date", "Date");

            segmentRow["FPNo"] = "FPNo";
            segmentRow["ReportNo"] = "ReportNo";
            segmentRow["RTNo"] = "RTNo";
            segmentRow["HeatNo"] = "HeatNo";
            segmentRow["Coverage"] = "Coverage";
            segmentRow["Date"] = "Date";

            string prevRTNo = "";

            //get the columns in order first
            string prevLocn = "";
            foreach (var locseg in report
                                    .SelectMany(p => p.Locations)
                                    .SelectMany(l => l.Segments.Select(s => new Tuple<string, string>(l.Location, s.Segment)))
                                    .Distinct()
                                    .OrderBy(q => q.Item1).ThenBy(q => q.Item2))
            {
                string header = String.Concat(locseg.Item1, "-", locseg.Item2);
                string colname = "col" + header.Replace("-", "");
                if (cols.FirstOrDefault(p => p.Caption == header) == null)
                {
                    AddTextColumn(reportTable, colname, header);
                    locationRow[colname] = locseg.Item1 == prevLocn ? "" : locseg.Item1; //do not get location get repeated
                    prevLocn = locseg.Item1;
                    segmentRow[colname] = locseg.Item2;
                }
            }

            lstExcelDataRows = new List<DataRow> {locationRow, segmentRow};

            foreach (var rt in report)
            {
                var row = new DataRow();
                var rowWithColor = new DataRow();

                row["FPNo"] = rowWithColor["FPNo"] = rt.FPNo; //txtFPNo.Text; //won't change throughout the report
                row["RTNo"] = rowWithColor["RTNo"] = prevRTNo == rt.RTNo ? "" : rt.RTNo; //prevent same RT No from repeating
                row["ReportNo"] = rowWithColor["ReportNo"] = rt.ReportNo;
                row["Coverage"] = rowWithColor["Coverage"] = rt.Coverage;
                row["HeatNo"] = rowWithColor["HeatNo"] = rt.HeatNo;
                prevRTNo = rt.RTNo;
                row["Date"] = rowWithColor["Date"] = rt.Date.ToString("dd/MM/yyyy");

                prevLocn = "";
                foreach (var loc in rt.Locations)
                {
                    foreach (var seg in loc.Segments)
                    {
                        string header = String.Concat(loc.Location, "-", seg.Segment);
                        string colname = "col" + header.Replace("-", "");
                        //no need to show NSD - not any more
                        row[colname] = seg.Observations.ToUpper();
                        string colourCode;
                        colourCode = RemarksColorCodeDic.ContainsKey(seg.RemarkText)
                                         ? RemarksColorCodeDic[seg.RemarkText]
                                         : string.Empty;
                        rowWithColor[colname] = seg.Observations.ToUpper() + colourCode;
                    }
                }
                rows.Add(row);
                lstExcelDataRows.Add(rowWithColor);
            }

            DataSet ds = new DataSet("ReportDataSet");
            ds.Tables.Add(reportTable);

            excelTable = new DataTable("ExcelReport");
            foreach (DataColumn column in reportTable.Columns)
            {
                excelTable.Columns.Add(column);
            }

            foreach (var row in lstExcelDataRows)
            {
               excelTable.Rows.Add(row);
            }

            reportGrid.DataSource = ds;
            reportGrid.DataMember = "Report";
            reportGrid.DataBind();

            var excelDs = new DataSet("ExcelDataSet");
            excelDs.Tables.Add(excelTable);
            reportGrid1 = new BindableDataGrid.BindableDataGrid {DataSource = excelDs, DataMember = "ExcelReport"};
            reportGrid1.DataBind();

            busyIndicator.IsBusy = false;
        }
        private void loadCompleted(object sender, EventArgs e)
        {
            //wait till fully loaded
            if (ctx.IsLoading) return;

            var operation = sender as LoadOperation<RGReport>;
            var rgReport = operation.Entities.FirstOrDefault();
            reportNumberToDownload = rgReport.ReportNo;

            var reportTable = new DataTable("Report");
            var cols = reportTable.Columns;
            var rows = reportTable.Rows;

            var headerRow = new DataRow();
            rows.Add(headerRow);

            var columnNames = new List<string>()
               {"SlNo","Location","Thickness","SFD","IQI Designation","IQI Sensitivity",
                "Density", "FilmSize","Observations","Classifactions","Remarks", "ReportNo",
                "ReportDate", "Film","RTNO","FPNO","HEATNO","DateOfTest","LeadScreenFront","LeadScreenBack",
                "Viewing","Source","SourceSize","Coverage","ProcedureReference","AcceptanceAsper",
                "TotalNoOfFilms"
               };

            foreach (var en in ctx.Energies)
            {
                columnNames.Add(en.Name + " Area");
            }

            columnNames.Add("Ir192_Co60 Strength");

            foreach (var columnName in columnNames)
            {
                AddTextColumn(reportTable, columnName, columnName);
            }

            foreach (var columnName in columnNames)
            {
                headerRow[columnName] = columnName;
            }

            var totalFilmCount = rgReport.RGReportRows.Sum(p => p.FilmCount);

            foreach (RGReportRow row in rgReport.RGReportRows)
            {
                var dataRow = new DataRow();
                dataRow["SlNo"] = rows.Count;
                dataRow["Location"] = row.LocationAndSegment ?? string.Empty;
                dataRow["Thickness"] = row.ThicknessRange;
                dataRow["SFD"] = row.SFD;
                dataRow["IQI Designation"] = row.Designation ?? string.Empty;
                dataRow["IQI Sensitivity"] = row.Sensitivity ?? string.Empty;
                dataRow["Density"] = row.Density ?? string.Empty;
                if (!IsFilmSizeInCms)
                    dataRow["FilmSize"] = row.FilmSizeString ?? string.Empty;
                else
                    dataRow["FilmSize"] = row.FilmSizeStringInCms ?? string.Empty;
                dataRow["Observations"] = row.Findings ?? string.Empty;
                dataRow["Classifactions"] = row.Classifications ?? string.Empty;
                dataRow["Remarks"] = row.RemarkText ?? string.Empty;
                dataRow["ReportNo"] = rgReport.ReportNo ?? string.Empty;
                dataRow["ReportDate"] = rgReport.ReportDate.ToShortDateString();
                dataRow["Film"] = rgReport.Film ?? string.Empty;
                dataRow["RTNO"] = rgReport.RTNo ?? string.Empty;
                dataRow["FPNO"] = rgReport.FixedPattern != null ? rgReport.FixedPattern.FPNo : string.Empty;
                dataRow["HEATNO"] = rgReport.HeatNo ?? string.Empty;
                dataRow["DateOfTest"] = rgReport.DateOfTest.ToShortDateString();
                dataRow["LeadScreenFront"] = rgReport.LeadScreen;
                dataRow["LeadScreenBack"] = rgReport.LeadScreenBack;
                dataRow["Viewing"] = rgReport.Viewing ?? string.Empty;
                dataRow["Source"] = rgReport.Source ?? string.Empty;
                dataRow["SourceSize"] = rgReport.SourceSize ?? string.Empty;
                dataRow["Coverage"] = rgReport.Coverage != null ? rgReport.Coverage.CoverageName : string.Empty;
                dataRow["ProcedureReference"] = rgReport.ProcedureRef ?? string.Empty;
                dataRow["AcceptanceAsper"] = rgReport.AcceptanceAsPer ?? string.Empty;
                dataRow["TotalNoOfFilms"] = row.FilmCount;    //totalFilmCount;
                dataRow["Ir192_Co60 Strength"] = rgReport.Strength ?? string.Empty;

                foreach (var en in ctx.Energies)
                {
                    if (!IsFilmSizeInCms)
                    {
                        dataRow[en.Name + " Area"] = rgReport.RGReportRows
                            .Where(p => p.EnergyID == en.ID &&
                                //  p.RemarkText != "RETAKE" &&
                                        p.SlNo == rows.Count)
                            .Sum(p => p.FilmSize.Area * p.FilmCount);
                    }
                    else
                    {
                        dataRow[en.Name + " Area"] = rgReport.RGReportRows
                           .Where(p => p.EnergyID == en.ID &&
                               //  p.RemarkText != "RETAKE" &&
                                       p.SlNo == rows.Count)
                           .Sum(p => p.FilmSize.AreaInCms * p.FilmCount);
                    }
                }

                rows.Add(dataRow);
            }

            var ds = new DataSet("ReportDataSet");
            ds.Tables.Add(reportTable);

            reportGrid = new BindableDataGrid.BindableDataGrid();
            reportGrid.DataSource = ds;
            reportGrid.DataMember = "Report";
            reportGrid.DataBind();

            busyIndicator.IsBusy = false;

            myPopup.IsOpen = true;
        }
        private void loadCompleted(object sender, EventArgs e)
        {
            //wait till fully loaded
            if (ctx.IsLoading) return;

            var report = ((LoadOperation<ShiftWisePerformanceRow>)sender).Entities;

            //Can't directly bind the report to the grid due to variable no of columns. Using a custom datagrid bindable
            //to a datatable instead

            reportTable = new DataTable("Report");
            var cols = reportTable.Columns;
            var rows = reportTable.Rows;

            DataRow headerRow = new DataRow();
            rows.Add(headerRow);

            DataRow subHeaderRow = new DataRow();
            rows.Add(subHeaderRow);

            AddTextColumn(reportTable, "Technicians", "Technicians");
            AddTextColumn(reportTable, "Date", "Date");
            AddTextColumn(reportTable, "Shift", "Shift");

            headerRow["Technicians"] = "Technicians";
            headerRow["Date"] = "Date";
            headerRow["Shift"] = "Shift";

            subHeaderRow["Technicians"] = "";
            subHeaderRow["Date"] = "";
            subHeaderRow["Shift"] = "";

            //filmsize columns
            foreach (var row in ctx.FilmSizes.OrderBy(p => p.Area))
            {
                var totalcol = "Film" + row.Name.Replace(".", "dot") + "Total";
                var rtcol = "Film" + row.Name.Replace(".", "dot") + "RT";
                AddTextColumn(reportTable, totalcol, totalcol);
                AddTextColumn(reportTable, rtcol, rtcol);
                headerRow[totalcol] = row.Name;
                headerRow[rtcol] = "";
                subHeaderRow[totalcol] = "Total";
                subHeaderRow[rtcol] = "RT";
            }

            AddTextColumn(reportTable, "TotalFilms", "Total Films Taken");
            AddTextColumn(reportTable, "TotalRetakes", "Total Retakes");
            AddTextColumn(reportTable, "RTPercent", "RT % by No");
            AddTextColumn(reportTable, "RTPercentByArea", "RT % by Area");

            headerRow["TotalFilms"] = "Total Films Taken";
            headerRow["TotalRetakes"] = "Total Retakes";
            headerRow["RTPercent"] = "RT % by No";
            headerRow["RTPercentByArea"] = "RT % by Area";

            subHeaderRow["TotalFilms"] = "";
            subHeaderRow["TotalRetakes"] = "";
            subHeaderRow["RTPercent"] = "";
            subHeaderRow["RTPercentByArea"] = "";

            foreach (var r in report)
            {
                DataRow row = new DataRow();
                row["Technicians"] = r.Technicians;
                row["Date"] = r.Date;
                row["Shift"] = r.Shift;

                foreach(var energy in r.FilmAreaRows)
                {
                    row["Film" + energy.FilmSize.Replace(".", "dot") + "Total"] = energy.Total;
                    row["Film" + energy.FilmSize.Replace(".", "dot") + "RT"] = energy.RT;
                }

                row["TotalFilms"] = r.TotalFilmsTaken;
                row["TotalRetakes"] = r.TotalRetakes;
                row["RTPercent"] = r.RTPercent.ToString("F2");
                row["RTPercentByArea"] = r.RTPercentByArea.ToString("F2");
                rows.Add(row);
            }

            DataSet ds = new DataSet("ReportDataSet");
            ds.Tables.Add(reportTable);

            reportGrid.DataSource = ds;
            reportGrid.DataMember = "Report";
            reportGrid.DataBind();

            busyIndicator.IsBusy = false;
        }