private void btnSalva_Click(object sender, EventArgs e)
        {
            try
            {
                int numeroGiorni = GetNumeroGiorni();
                foreach (DataRow riga in _dsGriglia.Tables[_nomeTabella].Rows)
                {
                    string IDMAGAZZLancio = (string)riga[(int)Colonne.IDMAGAZZLancio];
                    string IDMAGAZZFase   = (string)riga[(int)Colonne.IDMAGAZZFASE];
                    string reparto        = (string)riga[(int)Colonne.Reparto];
                    string fase           = (string)riga[(int)Colonne.Fase];

                    for (int i = 0; i < numeroGiorni; i++)
                    {
                        string barra  = (string)riga[(int)Colonne.NumeroPezzi + 1 + 3 * i + 2].ToString();
                        string valore = (string)riga[(int)Colonne.NumeroPezzi + 1 + 3 * i + 1].ToString();
                        valore = valore.Trim();
                        string   data = dgvGriglia.Columns[(int)Colonne.NumeroPezzi + 1 + 3 * i].Name;
                        DateTime dt   = DateTime.Parse(data);

                        PianificazioneDS.PIANIFICAZIONE_STATICARow rigaDaInserire = _dsPianificazione.PIANIFICAZIONE_STATICA.Where(x =>
                                                                                                                                   x.RowState != DataRowState.Deleted &&
                                                                                                                                   x.IDMAGAZZ == IDMAGAZZLancio &&
                                                                                                                                   x.IDMAGAZZ_FASE == IDMAGAZZFase &&
                                                                                                                                   x.CODICEFASE == fase &&
                                                                                                                                   x.REPARTO == reparto &&
                                                                                                                                   x.DATA == dt).FirstOrDefault();

                        if (!string.IsNullOrEmpty(valore))
                        {
                            if (rigaDaInserire == null)
                            {
                                rigaDaInserire               = _dsPianificazione.PIANIFICAZIONE_STATICA.NewPIANIFICAZIONE_STATICARow();
                                rigaDaInserire.CODICEFASE    = fase;
                                rigaDaInserire.DATA          = dt;
                                rigaDaInserire.IDMAGAZZ      = IDMAGAZZLancio;
                                rigaDaInserire.IDMAGAZZ_FASE = IDMAGAZZFase;
                                rigaDaInserire.QTA           = valore;
                                rigaDaInserire.REPARTO       = reparto;
                                _dsPianificazione.PIANIFICAZIONE_STATICA.AddPIANIFICAZIONE_STATICARow(rigaDaInserire);
                            }
                            else
                            {
                                rigaDaInserire.QTA = valore;
                            }
                        }
                        else
                        {
                            if (rigaDaInserire != null)
                            {
                                rigaDaInserire.Delete();
                            }
                        }
                    }
                }

                using (PianificazioneBusiness bPianificazione = new PianificazioneBusiness())
                {
                    bPianificazione.SalvaPianificazioneStatica(_dsPianificazione);
                    _dsPianificazione.AcceptChanges();
                }

                MessageBox.Show("Salvataggio riuscito", "Informazione", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERRORE", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void CreaDSGriglia()
        {
            _dsGriglia = new DataSet();
            DataTable dtGriglia = _dsGriglia.Tables.Add();

            dtGriglia.TableName = _nomeTabella;
            dtGriglia.Columns.Add("IDMAGAZZFASE", Type.GetType("System.String")).ReadOnly   = true;
            dtGriglia.Columns.Add("IDMAGAZZLancio", Type.GetType("System.String")).ReadOnly = true;
            dtGriglia.Columns.Add("Segnalatore", Type.GetType("System.String")).ReadOnly    = true;

            dtGriglia.Columns.Add("Modello lancio", Type.GetType("System.String")).ReadOnly = true;
            dtGriglia.Columns.Add("Descrizione", Type.GetType("System.String")).ReadOnly    = true;
            dtGriglia.Columns.Add("Modello", Type.GetType("System.String")).ReadOnly        = true;
            dtGriglia.Columns.Add("Reparto", Type.GetType("System.String")).ReadOnly        = true;
            dtGriglia.Columns.Add("Fase", Type.GetType("System.String")).ReadOnly           = true;
            dtGriglia.Columns.Add("Materiale", Type.GetType("System.String")).ReadOnly      = true;
            dtGriglia.Columns.Add("Finitura", Type.GetType("System.String")).ReadOnly       = true;
            dtGriglia.Columns.Add("Pezzi x barra", Type.GetType("System.String"));
            dtGriglia.Columns.Add("Gruppo", Type.GetType("System.String"));
            dtGriglia.Columns.Add("Pezzi pianificati", Type.GetType("System.Decimal"));
            dtGriglia.Columns.Add("Numero pezzi", Type.GetType("System.Decimal"));

            int numeroGiorni = GetNumeroGiorni();

            for (int i = 0; i < numeroGiorni; i++)
            {
                dtGriglia.Columns.Add(dtDal.Value.AddDays(i).ToShortDateString(), Type.GetType("System.Decimal")).ReadOnly = true;
                dtGriglia.Columns.Add(dtDal.Value.AddDays(i).ToShortDateString() + " statico", Type.GetType("System.String"));
                dtGriglia.Columns.Add(dtDal.Value.AddDays(i).ToShortDateString() + " barre", Type.GetType("System.String"));
            }

            var gruppi =
                from articolo in _dsPianificazione.V_PIAN_AGGR_2
                group articolo by new
            {
                articolo.IDMAGAZZ,
                articolo.IDMAGAZZ_FASE,
                articolo.SEGNALATORE,
                articolo.MODELLOLANCIO,
                articolo.MODELLO,
                articolo.DESCRIZIONE,
                articolo.REPARTO,
                articolo.CODICEFASE,
                articolo.MATERIALE,
                articolo.FINITURA,
                articolo.PEZZI,
                articolo.GRUPPO
            }
            into GruppiArticolo
            select GruppiArticolo.Key;

            foreach (var gruppo in gruppi)
            {
                DataRow riga = dtGriglia.NewRow();

                riga[(int)Colonne.IDMAGAZZFASE]   = gruppo.IDMAGAZZ_FASE;
                riga[(int)Colonne.IDMAGAZZLancio] = gruppo.IDMAGAZZ;
                riga[(int)Colonne.Segnalatore]    = gruppo.SEGNALATORE;
                riga[(int)Colonne.ModelloLancio]  = gruppo.MODELLOLANCIO;
                riga[(int)Colonne.Modello]        = gruppo.MODELLO;
                riga[(int)Colonne.Descrizione]    = gruppo.DESCRIZIONE;
                riga[(int)Colonne.Reparto]        = gruppo.REPARTO;
                riga[(int)Colonne.Fase]           = gruppo.CODICEFASE;
                riga[(int)Colonne.Materiale]      = gruppo.MATERIALE;
                riga[(int)Colonne.Finitura]       = gruppo.FINITURA;
                riga[(int)Colonne.PezziBarra]     = gruppo.PEZZI.ToString();
                if (gruppo.MATERIALE != "OTTONE")
                {
                    riga[(int)Colonne.Gruppo] = (1000000 + gruppo.GRUPPO).ToString();
                }
                else
                {
                    riga[(int)Colonne.Gruppo] = gruppo.GRUPPO.ToString();
                }

                decimal totale        = 0;
                decimal totaleStatico = 0;
                for (int i = 0; i < numeroGiorni; i++)
                {
                    PianificazioneDS.PIANIFICAZIONE_STATICARow statico = _dsPianificazione.PIANIFICAZIONE_STATICA.Where(x =>
                                                                                                                        x.IDMAGAZZ == gruppo.IDMAGAZZ &&
                                                                                                                        x.IDMAGAZZ_FASE == gruppo.IDMAGAZZ_FASE &&
                                                                                                                        x.CODICEFASE == gruppo.CODICEFASE &&
                                                                                                                        x.REPARTO == gruppo.REPARTO &&
                                                                                                                        x.DATA == dtDal.Value.AddDays(i)).FirstOrDefault();
                    string valoreStatico = string.Empty;
                    if (statico != null)
                    {
                        valoreStatico = statico.QTA;
                    }

                    if (i == 0)
                    {
                        decimal aux =
                            _dsPianificazione.V_PIAN_AGGR_2.Where(x =>
                                                                  x.IDMAGAZZ == gruppo.IDMAGAZZ &&
                                                                  x.IDMAGAZZ_FASE == gruppo.IDMAGAZZ_FASE &&
                                                                  x.SEGNALATORE == gruppo.SEGNALATORE &&
                                                                  x.REPARTO == gruppo.REPARTO &&
                                                                  x.CODICEFASE == gruppo.CODICEFASE &&
                                                                  x.DATAINIZIO <= dtDal.Value).Sum(x => x.QTA);

                        totale += aux;
                        decimal auxStatico;
                        if (decimal.TryParse(valoreStatico, out auxStatico))
                        {
                            totaleStatico += auxStatico;
                        }

                        riga[(int)Colonne.NumeroPezzi + 1]     = aux.ToString();
                        riga[(int)Colonne.NumeroPezzi + 1 + 1] = valoreStatico;
                        if (statico != null)
                        {
                            riga[(int)Colonne.NumeroPezzi + 1 + i * 3 + 2] = CalcolaBarre(gruppo.PEZZI.ToString(), decimal.Parse(statico.QTA));
                        }
                        else
                        {
                            riga[(int)Colonne.NumeroPezzi + 1 + i * 3 + 2] = string.Empty;
                        }
                    }
                    else
                    {
                        decimal aux = _dsPianificazione.V_PIAN_AGGR_2.Where(x =>
                                                                            x.IDMAGAZZ == gruppo.IDMAGAZZ &&
                                                                            x.IDMAGAZZ_FASE == gruppo.IDMAGAZZ_FASE &&
                                                                            x.SEGNALATORE == gruppo.SEGNALATORE &&
                                                                            x.REPARTO == gruppo.REPARTO &&
                                                                            x.CODICEFASE == gruppo.CODICEFASE &&
                                                                            x.DATAINIZIO == dtDal.Value.AddDays(i)).Sum(x => x.QTA);

                        totale += aux;
                        decimal auxStatico;
                        if (decimal.TryParse(valoreStatico, out auxStatico))
                        {
                            totaleStatico += auxStatico;
                        }

                        riga[(int)Colonne.NumeroPezzi + 1 + i * 3]     = aux;
                        riga[(int)Colonne.NumeroPezzi + 1 + i * 3 + 1] = valoreStatico;
                        if (statico != null)
                        {
                            riga[(int)Colonne.NumeroPezzi + 1 + i * 3 + 2] = CalcolaBarre(gruppo.PEZZI.ToString(), decimal.Parse(statico.QTA));
                        }
                        else
                        {
                            riga[(int)Colonne.NumeroPezzi + 1 + i * 3 + 2] = string.Empty;
                        }
                    }
                }
                if (totale > 0)
                {
                    riga[(int)Colonne.PezziPianificati] = totale;
                }
                if (totaleStatico > 0)
                {
                    riga[(int)Colonne.NumeroPezzi] = totaleStatico;
                }

                dtGriglia.Rows.Add(riga);
            }
        }