Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较、添加、删除或更新两个datagridview c之间的行#_C#_Excel_Winforms_Datagridview - Fatal编程技术网

C# 比较、添加、删除或更新两个datagridview c之间的行#

C# 比较、添加、删除或更新两个datagridview c之间的行#,c#,excel,winforms,datagridview,C#,Excel,Winforms,Datagridview,我的第二个C#winform程序有问题。 在主窗体中,我使用以下方式从excel文件填充datagridview(dataGridView1): string percorsoConnessione = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + percorsoFile.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";

我的第二个C#winform程序有问题。 在主窗体中,我使用以下方式从excel文件填充datagridview(dataGridView1):

                string percorsoConnessione = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + percorsoFile.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
                OleDbConnection connessione = new OleDbConnection(percorsoConnessione);
                connessione.Open();
                DataTable selezionaFoglioExcel = connessione.GetSchema("Tables");
                string foglioExcel = selezionaFoglioExcel.Rows[0]["TABLE_NAME"].ToString();
                OleDbDataAdapter sceltaDati = new OleDbDataAdapter("SELECT * FROM [" + foglioExcel + "] WHERE [Cella] ='" + sceltaCella.Text + "'", connessione);
                sceltaDati.Fill(datiDataSet);
                dataGridView1.DataSource = datiDataSet;
            string percorsoConnessione = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + percorsoFile.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
            OleDbConnection connessione = new OleDbConnection(percorsoConnessione);
            connessione.Open();
            DataTable selezionaFoglioExcel = connessione.GetSchema("Tables");
            string foglioExcel = selezionaFoglioExcel.Rows[0]["TABLE_NAME"].ToString();
            OleDbDataAdapter sceltaDatiUpdate = new OleDbDataAdapter("SELECT * FROM [" + foglioExcel + "] WHERE [Cella] ='" + sceltaCella.Text + "'", connessione);
            DataTable datiDataSetUpdate = new DataTable();
            sceltaDatiUpdate.Fill(datiDataSetUpdate);
            dataGridView2.DataSource = datiDataSetUpdate;
在隐藏的datagridview(dataGridView2)中,我有一个更新的excel文件,该文件以以下方式填充此DGV:

                string percorsoConnessione = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + percorsoFile.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
                OleDbConnection connessione = new OleDbConnection(percorsoConnessione);
                connessione.Open();
                DataTable selezionaFoglioExcel = connessione.GetSchema("Tables");
                string foglioExcel = selezionaFoglioExcel.Rows[0]["TABLE_NAME"].ToString();
                OleDbDataAdapter sceltaDati = new OleDbDataAdapter("SELECT * FROM [" + foglioExcel + "] WHERE [Cella] ='" + sceltaCella.Text + "'", connessione);
                sceltaDati.Fill(datiDataSet);
                dataGridView1.DataSource = datiDataSet;
            string percorsoConnessione = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + percorsoFile.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
            OleDbConnection connessione = new OleDbConnection(percorsoConnessione);
            connessione.Open();
            DataTable selezionaFoglioExcel = connessione.GetSchema("Tables");
            string foglioExcel = selezionaFoglioExcel.Rows[0]["TABLE_NAME"].ToString();
            OleDbDataAdapter sceltaDatiUpdate = new OleDbDataAdapter("SELECT * FROM [" + foglioExcel + "] WHERE [Cella] ='" + sceltaCella.Text + "'", connessione);
            DataTable datiDataSetUpdate = new DataTable();
            sceltaDatiUpdate.Fill(datiDataSetUpdate);
            dataGridView2.DataSource = datiDataSetUpdate;
现在我需要的是:

  • 如果对于第二个DGV中的每一行,第一个DGV中存在名为“ODL”的列中的值,我需要更新第一个DGV中“Qta Ordine”、“Qta da stampare”、“Qta stampata”和“Warehouse date”列中单元格的值 使用从第二个DGV获取的值
  • 如果对于第二个DGV中的每一行,“ODL”列中的值在第一个DGV中不存在,我需要将该行添加到第一个DGV中,并复制从第二个DGV中获取的值
  • 如果对于第一个DGV中的每一行,“ODL”列中的值在第二个DGV中不存在,我需要删除第一个DGV中的行。 我希望有人能帮助我。 如果我犯了语法错误或者不清楚,请原谅,我的英语很生疏。 谢谢大家,祝大家快乐。 吉安·卢卡
  • 编辑: 对于第一个问题,我这样做了,它似乎有效:

            int numeroRigheDGV1 = dataGridView1.RowCount;
            int numeroRigheDGV2 = dataGridView2.RowCount;
    
            for (int i = 0; i < numeroRigheDGV1; i++)
            {
                for (int j = 0; j < numeroRigheDGV2; j++)
                {
                    if (dataGridView2["ODL", j].Value.ToString() == dataGridView1["ODL", i].Value.ToString())
                    {
                        dataGridView1["Warehouse date", i].Value = dataGridView2["Warehouse date", j].Value;
                        dataGridView1["Qta da stampare", i].Value = dataGridView2["Qta da stampare", j].Value;
                        dataGridView1["Qta stampata", i].Value = dataGridView2["Qta stampata", j].Value;
                    }
                }
            }
    
    int numerighedgv1=dataGridView1.RowCount;
    int numerighedgv2=dataGridView2.RowCount;
    对于(int i=0;i
    这就是我所做的
    我终于解决了我的问题。这是我的解决方案:

            int indice = 0; 
            int rimosse = 0;
            int aggiornate = 0;
            int aggiunte = 0;
            int righeDGV1 = dataGridView1.Rows.Count;
            int righeDGV2 = dataGridView2.Rows.Count;
            for (int i = 0; i < righeDGV1; i++) //Loop in DGV1
            {
                int righedaSaltare = 0;
                string controlloODL = dataGridView1["ODL", indice].Value.ToString();
                for (int j = 0; j < righeDGV2; j++) //Check if the row in DGV1 is present in DGV2
                {
                    if (controlloODL == dataGridView2["ODL", j].Value.ToString()) 
                    {
                        //If true increase the counters skipline and update, then update the value of the row in DGV1
                        righedaSaltare++;
                        aggiornate++;
                        dataGridView1["Qta ordine", indice].Value = dataGridView2["Qta ordine", j].Value;
                        dataGridView1["Qta stampata", indice].Value = dataGridView2["Qta stampata", j].Value;
                        dataGridView1["Qta da stampare", indice].Value = dataGridView2["Qta da stampare", j].Value;
                        dataGridView1["Warehouse date", indice].Value = dataGridView2["Warehouse date", j].Value;
                    }
                }
                if (righedaSaltare == 0)
                    //If the row in DGV1 isn't in DGV2 remove it 
                {
                    dataGridView1.Rows.RemoveAt(indice);
                    rimosse++;
                }
                if (righedaSaltare > 0)
                {
                    //If the row in DGV1 is present in DGV2, check the next line in DGV1
                    indice++;
                }
            } 
            int nuovoindice = 0;
            int righeDGV1nuovo = dataGridView1.RowCount;
            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {
                //Loop in DGV2
                int righedaSaltare = 0;
                string controlloNuovoODL = dataGridView2["ODL", nuovoindice].Value.ToString();
                for (int k = 0; k < dataGridView1.Rows.Count; k++)
                {
                    //Check if the row in DGV2 is present in DGV1
                    if (controlloNuovoODL == dataGridView1["ODL", k].Value.ToString())
                    {
                        //If true increase the counter skipline
                        righedaSaltare++;
                    }
                }
                if (righedaSaltare == 0)
                {
                    //If the row in DGV2 isn't in DGV1, add it to datatable, increase the counter update and re-sort the DGV1
                    DataRow nuovaRigaODL = datiDataSet.NewRow();
                    nuovaRigaODL["Giorni utili"] = dataGridView2["Giorni utili", nuovoindice].Value.ToString();
                    nuovaRigaODL["Giorni necessari"] = dataGridView2["Giorni necessari", nuovoindice].Value.ToString();
                    nuovaRigaODL["Sequenza"] = dataGridView2["Sequenza", nuovoindice].Value.ToString();
                    nuovaRigaODL["Montato"] = dataGridView2["Montato", nuovoindice].Value;
                    nuovaRigaODL["Cella"] = dataGridView2["Cella", nuovoindice].Value.ToString();
                    nuovaRigaODL["Data inizio stampaggio"] = dataGridView2["Data inizio stampaggio", nuovoindice].Value.ToString();
                    nuovaRigaODL["Warehouse date"] = dataGridView2["Warehouse date", nuovoindice].Value.ToString();
                    nuovaRigaODL["ODL"] = dataGridView2["ODL", nuovoindice].Value.ToString();
                    nuovaRigaODL["Item number"] = dataGridView2["Item number", nuovoindice].Value.ToString();
                    nuovaRigaODL["Qta ordine"] = dataGridView2["Qta ordine", nuovoindice].Value.ToString();
                    nuovaRigaODL["Qta stampata"] = dataGridView2["Qta stampata", nuovoindice].Value.ToString();
                    nuovaRigaODL["Qta da stampare"] = dataGridView2["Qta da stampare", nuovoindice].Value.ToString();
                    nuovaRigaODL["Pressa"] = dataGridView2["Pressa", nuovoindice].Value.ToString();
                    nuovaRigaODL["Trimming"] = dataGridView2["Trimming", nuovoindice].Value.ToString();
                    nuovaRigaODL["Materiale"] = dataGridView2["Materiale", nuovoindice].Value.ToString();
                    nuovaRigaODL["Stampo"] = dataGridView2["Stampo", nuovoindice].Value.ToString();
                    nuovaRigaODL["Diametro"] = dataGridView2["Diametro", nuovoindice].Value.ToString();
                    nuovaRigaODL["Impr Disp"] = dataGridView2["Impr Disp", nuovoindice].Value.ToString();
                    nuovaRigaODL["Peso pezzo (grammi)"] = dataGridView2["Peso pezzo (grammi)", nuovoindice].Value.ToString();
                    nuovaRigaODL["Peso materozza (grammi)"] = dataGridView2["Peso materozza (grammi)", nuovoindice].Value.ToString();
                    nuovaRigaODL["Consumo materiale (Kg)"] = dataGridView2["Consumo materiale (Kg)", nuovoindice].Value.ToString();
                    aggiunte++;
                    datiDataSet.Rows.Add(nuovaRigaODL);
                    datiDataSet.DefaultView.Sort = "Warehouse date";
                    i--;
                }
                if (righedaSaltare > 0)
                {
                    //If the row exist in DGV1, check the next row in DGV2
                    nuovoindice++;
                }
            }
    
    int indice=0;
    int-rimosse=0;
    int aggiornate=0;
    int aggiunte=0;
    int righeDGV1=dataGridView1.Rows.Count;
    int righeDGV2=dataGridView2.Rows.Count;
    对于(int i=0;i0)
    {
    //如果DGV1中的行存在于DGV2中,请检查DGV1中的下一行
    indice++;
    }
    } 
    int nuovoindice=0;
    int righeDGV1nuovo=dataGridView1.RowCount;
    对于(int i=0;i