private void btREdit_Click(object sender, EventArgs e) { using (FrmSelectFormula f = new FrmSelectFormula(oFormulaCollection.ToArray())) { f.ShowDialog(); if (f.DialogResult == System.Windows.Forms.DialogResult.OK) { List <IItem> formulas = f._Formulas.ToList(); //整理資料表 //移除不在新項目的欄位 for (int i = oFormulaCollection.Count; i-- > 0;) { if (!ItemFunction.CompareFormulationCollection(formulas, oFormulaCollection[i])) { IItem item = oFormulaCollection[i]; string name = item.Name + "[" + item.Unit + "]"; int rowid = dgViewFormula.Rows.IndexOf(dgViewFormula.Rows.Cast <DataGridViewRow>().FirstOrDefault(r => r.Cells[0].Value.ToString() == name)); dgViewFormula.Rows.RemoveAt(rowid); oFormulaCollection.RemoveAt(i); } } //新增欄位於表格中 DataGridViewRow row; for (int i = 0; i < formulas.Count; i++) { if (oFormulaCollection.Count == 0 || !ItemFunction.CompareFormulationCollection(oFormulaCollection, formulas[i])) { IItem item = formulas[i]; string name = item.Name + "[" + item.Unit + "]"; row = new DataGridViewRow(); row.Cells.Add(new DataGridViewTextBoxCell()); row.Cells[0].Value = item.ID; row.Cells.Add(new DataGridViewTextBoxCell()); row.Cells[1].Value = name; dgViewFormula.Rows.Insert(i, row); } } oFormulaCollection = formulas; } } }