public static void FillOutGrid(DataGridView grid, DataTable table, bool attachImages)
        {
            grid.Columns.Clear();
            grid.Columns.Add("Nazwa", "Nazwa");

            for (int r = 0; r < table.Rows.Count; r++)
            {
                grid.Columns.Add("Wartosc" + r, "Wartosc" + r);

                for (int c = 0; c < table.Columns.Count; c++)
                {
                    grid.Rows.Add(table.Columns[c].ColumnName, table.Rows[r][c]);
                }
                if (attachImages)
                {
                    DataGridViewImageButtonSaveColumn columnImage = new DataGridViewImageButtonSaveColumn();

                    columnImage.Name       = "Images";
                    columnImage.HeaderText = "";
                    grid.Columns.Add(columnImage);

                    string date        = table.Rows[r]["Data_czas"].ToString().Substring(0, 10).Replace(".", "-");
                    string lot         = table.Rows[r]["numerZlecenia"].ToString();
                    var    fileImgList = VIOperations.TryGetFileInfoOfImagesForLot(lot, date);

                    if (fileImgList.Count > 0)
                    {
                        foreach (DataGridViewRow row in grid.Rows)
                        {
                            string rowFailureName             = row.Cells[0].Value.ToString();
                            List <System.IO.FileInfo> tagList = new List <System.IO.FileInfo>();
                            foreach (var img in fileImgList)
                            {
                                string failureName = img.Name.Split('_')[1];


                                if (failureName == rowFailureName)
                                {
                                    tagList.Add(img);
                                }
                            }
                            if (tagList.Count > 0)
                            {
                                ((DataGridViewImageButtonSaveCell)(row.Cells[r + 2])).Enabled     = true;
                                ((DataGridViewImageButtonSaveCell)(row.Cells[r + 2])).ButtonState = PushButtonState.Normal;

                                row.Cells[r + 2].Tag = tagList;
                            }
                        }
                    }
                }
            }



            SMTOperations.autoSizeGridColumns(grid);
        }
        private void WasteReasonDetails_Load(object sender, EventArgs e)
        {
            DataTable sourceTable = new DataTable();

            sourceTable.Columns.Add("Dobrych");
            sourceTable.Columns.Add("Ng");
            sourceTable.Columns.Add("LOT");
            sourceTable.Columns.Add("Model");
            sourceTable.Columns.Add("Data");
            sourceTable.Columns.Add("LiniaSMT");
            sourceTable.Columns.Add("Operator");
            //sourceTable.Columns.Add("Zdjecia");


            sourceTable.Columns["Dobrych"].DataType = typeof(Int32);

            Dictionary <string, Int32> qtyPerModel = new Dictionary <string, int>();
            Dictionary <string, Int32> qtyPerLine  = new Dictionary <string, int>();

            foreach (var lot in inputWasteData.Lots)
            {
                string model = lot.Model;
                string line  = lot.SmtLine;

                if (!qtyPerLine.ContainsKey(line))
                {
                    qtyPerLine.Add(line, 0);
                }
                if (!qtyPerModel.ContainsKey(model))
                {
                    qtyPerModel.Add(model, 0);
                }

                Int32 qty = lot.WastePerReason[title];
                qtyPerLine[line]   += qty;
                qtyPerModel[model] += qty;

                var imageList = VIOperations.TryGetFileInfoOfImagesForLot(lot.NumerZlecenia, lot.RealDateTime.ToString("dd-MM-yyyy"));
                if (imageList.Count > 0)
                {
                    if (!imagesPerLot.ContainsKey(lot.NumerZlecenia))
                    {
                        imagesPerLot.Add(lot.NumerZlecenia, new List <FileInfo>());
                    }
                    imagesPerLot[lot.NumerZlecenia].AddRange(imageList);
                }

                sourceTable.Rows.Add(lot.GoodQty, lot.WastePerReason[title], lot.NumerZlecenia, lot.Model, lot.RealDateTime, lot.SmtLine, lot.Oper);
            }

            DataView dv = sourceTable.DefaultView;

            dv.Sort = "Dobrych desc";
            dataGridView1.DataSource = dv.ToTable();

            label1.Text = title;

            DataTable modelSource = new DataTable();

            modelSource.Columns.Add("Model");
            modelSource.Columns.Add("Ilość", typeof(Int32));

            DataTable lineSource = new DataTable();

            lineSource.Columns.Add("Linia");
            lineSource.Columns.Add("Ilość", typeof(Int32));

            foreach (var modelEntry in qtyPerModel)
            {
                modelSource.Rows.Add(modelEntry.Key, modelEntry.Value);
            }

            foreach (var lineEntry in qtyPerLine)
            {
                lineSource.Rows.Add(lineEntry.Key, lineEntry.Value);
            }

            dataGridViewModel.DataSource = modelSource;
            dataGridViewLine.DataSource  = lineSource;

            SMTOperations.autoSizeGridColumns(dataGridView1);
            SMTOperations.autoSizeGridColumns(dataGridViewLine);
            SMTOperations.autoSizeGridColumns(dataGridViewModel);

            dataGridViewLine.Sort(this.dataGridViewLine.Columns["Ilość"], ListSortDirection.Descending);
            dataGridViewModel.Sort(this.dataGridViewModel.Columns["Ilość"], ListSortDirection.Descending);
        }
        public static void ReLoadViTab(
            ref Dictionary <string, string> lotModelDictionary,
            ref List <WasteDataStructure> inspectionData,
            ref List <excelOperations.order12NC> mstOrders,
            ComboBox comboBoxViOperatorsCapa,
            ComboBox comboBoxModel,
            ComboBox comboBoxViModelAnalFamily,
            ComboBox comboBoxViModelAnalModel,

            CheckedListBox checkedListBoxViWasteLevelSmtLines,
            CheckedListBox checkedListBoxViReasons,
            CheckedListBox cBListViReasonAnalysesSmtLines,
            CheckedListBox cBListViModelAnalysesSmtLines,
            CheckedListBox cBListViReasonList,

            DateTimePicker dateTimePickerPrzyczynyOdpaduOd,
            DateTimePicker dateTimePickerWasteLevelBegin,
            DataGridView dataGridViewDuplikaty,
            DataGridView dataGridViewPomylkiIlosc,
            DataGridView dataGridViewPowyzej50,
            DataGridView dataGridViewBledyNrZlec,
            DataGridView dataGridViewMstOrders,
            DataGridView dataGridViewViOperatorsTotal,
            DataGridView gridLatestLots,
            DateTimePicker dateTimePickerViOperatorEfiiciencyStart,
            DateTimePicker dateTimePickerViOperatorEfiiciencyEnd,
            NumericUpDown numericUpDownMoreThan50Scrap,
            NumericUpDown numericUpDownMoreThan50Ng,
            Dictionary <string, string> lotToOrderedQty,

            //rework
            DataGridView dataGridViewReworkDailyReport,
            DataGridView dataGridViewReworkByOperator,
            DataGridView dataGridViewServiceVsNg,
            Chart chartServiceVsNg,
            bool chartDaily
            )
        {
            mstOrders = excelOperations.loadExcel(ref lotModelDictionary);

            if (masterVITable.Rows.Count < 1)
            {
                masterVITable = SQLoperations.DownloadVisInspFromSQL(120);
            }

            //textBox1.Text += "SQL table: " + masterVITable.Rows.Count + " rows" + Environment.NewLine;
            comboBoxViOperatorsCapa.Items.AddRange(CreateOperatorsList(masterVITable).ToArray());
            lotToSmtLine   = SQLoperations.lotToSmtLine(80); // to remove???
            inspectionData = ViDataLoader.LoadData(masterVITable, lotToSmtLine, lotModelDictionary);

            string[] smtLines = lotToSmtLine.Select(l => l.Value).Distinct().OrderBy(o => o).ToArray();

            foreach (var smtLine in smtLines)
            {
                checkedListBoxViWasteLevelSmtLines.Items.Add(smtLine, true);

                checkedListBoxViReasons.Items.Add(smtLine, true);
                cBListViReasonAnalysesSmtLines.Items.Add(smtLine, true);
                cBListViModelAnalysesSmtLines.Items.Add(smtLine, true);
            }

            comboBoxModel.Items.AddRange(lotModelDictionary.Select(m => m.Value.Replace("LLFML", "")).Distinct().OrderBy(o => o).ToArray());

            dateTimePickerPrzyczynyOdpaduOd.Value = DateTime.Now.AddDays(-30);
            dateTimePickerWasteLevelBegin.Value   = DateTime.Now.AddDays(-30);
            comboBoxViOperatorsCapa.SelectedIndex = comboBoxViOperatorsCapa.Items.IndexOf("Wszyscy");

            dataGridViewDuplikaty.DataSource = SzukajDuplikatow(inspectionData);
            dgvTools.ColumnsAutoSize(dataGridViewDuplikaty, DataGridViewAutoSizeColumnMode.AllCells);
            dataGridViewDuplikaty.Sort(dataGridViewDuplikaty.Columns[0], ListSortDirection.Descending);
            dgvTools.ColumnsAutoSize(dataGridViewDuplikaty, DataGridViewAutoSizeColumnMode.AllCells);

            dataGridViewPomylkiIlosc.DataSource = PomylkiIlosci(lotToOrderedQty, inspectionData);
            dgvTools.ColumnsAutoSize(dataGridViewPomylkiIlosc, DataGridViewAutoSizeColumnMode.AllCellsExceptHeader);

            dataGridViewPowyzej50.DataSource = MoreThan50(numericUpDownMoreThan50Scrap, numericUpDownMoreThan50Ng, lotModelDictionary, inspectionData);
            dgvTools.ColumnsAutoSize(dataGridViewPowyzej50, DataGridViewAutoSizeColumnMode.AllCells);
            dataGridViewPowyzej50.Sort(dataGridViewPowyzej50.Columns["Ile"], ListSortDirection.Descending);


            HashSet <string> wasteReasonList = new HashSet <string>();

            foreach (var wasteReason in inspectionData)
            {
                foreach (var r in wasteReason.WastePerReason)
                {
                    wasteReasonList.Add(r.Key.Replace("ng", "").Replace("scrap", ""));
                }
                break;
            }

            cBListViReasonList.Items.AddRange(wasteReasonList.ToArray());



            comboBoxViModelAnalFamily.Items.AddRange(modelFamilyList(inspectionData, lotModelDictionary));
            comboBoxViModelAnalModel.Items.AddRange(uniqueModelsList(inspectionData, lotModelDictionary));

            dataGridViewBledyNrZlec.DataSource = UnknownOrderNumberTable(lotModelDictionary, inspectionData);

            VIOperations.ngRatePerOperator(inspectionData, dateTimePickerViOperatorEfiiciencyStart.Value, dateTimePickerViOperatorEfiiciencyEnd.Value, dataGridViewViOperatorsTotal);

            SMTOperations.autoSizeGridColumns(dataGridViewViOperatorsTotal);

            dataGridViewMstOrders.DataSource = VIOperations.checkMstViIfDone(mstOrders, inspectionData);

            Rework.FillOutGridDailyProdReport(dataGridViewReworkDailyReport, dataGridViewReworkByOperator, SQLoperations.GetLedRework());
            RefreshReworkChart(inspectionData, chartServiceVsNg, chartDaily, dataGridViewServiceVsNg);

            FillOutGridLatestLots(gridLatestLots, inspectionData);
        }