在两个datagridview c#

时间:2016-03-10 21:42:27

标签: 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;\";";
                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;

在隐藏的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 sceltaDatiUpdate = new OleDbDataAdapter("SELECT * FROM [" + foglioExcel + "] WHERE [Cella] ='" + sceltaCella.Text + "'", connessione);
            DataTable datiDataSetUpdate = new DataTable();
            sceltaDatiUpdate.Fill(datiDataSetUpdate);
            dataGridView2.DataSource = datiDataSetUpdate;

现在我需要的是:

  1. 如果对于第二个DGV中的每一行,名为“ODL”的列中存在的值存在于第一个DGV中,我需要更新“Qta Ordine”,“Qta da stampare”,“Qta stampata”列中的单元格值第一个DGV中的“和”仓库日期“ 使用第二个DGV的值;
  2. 如果对于第二个DGV中的每一行,第一个DGV中不存在“ODL”列中存在的值,我需要在第一个DGV中添加该行并复制从第二个DGV获取的值;
  3. 如果对于第一个DGV中的每一行,第二个DGV中不存在“ODL”列中存在的值,我需要删除第一个DGV中的行。 我希望有人可以帮助我。 附:对不起,如果我犯了语法错误或者我不清楚,但我的英语非常生疏。 感谢所有人的快乐编码。 吉安卢卡
  4. 编辑: 对于第一个问题,我做了这个,似乎有效:

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

    这就是我所做的 Scheduler

1 个答案:

答案 0 :(得分:0)

我终于解决了我的问题。这是我的解决方案:

        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++;
            }
        }

也许不优雅但它对我有用。 我希望我能用这段代码帮助任何人。 感谢。