private void btn_Save_Click(object sender, EventArgs e) { IDomain domain = null; IRangeDomain rd = null; ICodedValueDomain cv = null; IDomainFactory df = new DomainFactory(); DataTable table = null; try { //保存时,删除操作才生效 foreach (string s in deletedomains) { ds.DeleteDomain(s); } for (int i = 0; i < this.dgv_DomainAttr.Rows.Count; i++) { DataGridViewRow myRow = this.dgv_DomainAttr.Rows[i]; string name = myRow.Cells["name"].Value == null ? "" : myRow.Cells["name"].Value.ToString(); string description = myRow.Cells["description"].Value == null ? "" : myRow.Cells["description"].Value.ToString(); string ft = myRow.Cells["fieldtype"].Value == null ? "" : myRow.Cells["fieldtype"].Value.ToString(); string dt = myRow.Cells["domaintype"].Value == null ? "" : myRow.Cells["domaintype"].Value.ToString(); if (name.Equals("")) { continue; //名称为空,不创建 } if (myRow.Tag == null) { continue; } if (ft.Equals("String") && dt.Equals("值域型")) { continue; } table = myRow.Tag as DataTable; if (!HasDomain(name, ds)) { if (dt == "值域型") { rd = df.CreateRangeDomain(name, GetFDEFieldTypeByString(ft)); rd.Description = description; rd.MinValue = table.Rows[0].IsNull(sMinFieldName) ? GetColumnDefaultValue(ft) : table.Rows[0][sMinFieldName]; rd.MaxValue = table.Rows[0].IsNull(sMaxFieldName) ? GetColumnDefaultValue(ft) : table.Rows[0][sMaxFieldName]; ds.AddDomain(rd); } else if (dt == "枚举型") { cv = df.CreateCodedValueDomain(name, GetFDEFieldTypeByString(ft)); cv.Description = description; for (int j = 0; j < table.Rows.Count; j++) { if (table.Rows[j][sDscribFieldName].ToString() != "") { if (table.Rows[j].IsNull(sEmunFieldName)) { continue; } cv.AddCode(table.Rows[j][sEmunFieldName], table.Rows[j][sDscribFieldName].ToString()); } } ds.AddDomain(cv); } } else { domain = ds.GetDomainByName(name); if (dt == "值域型") { rd = domain as IRangeDomain; rd.Description = description; rd.MaxValue = table.Rows[0].IsNull(sMaxFieldName) ? GetColumnDefaultValue(ft) : table.Rows[0][sMaxFieldName]; rd.MinValue = table.Rows[0].IsNull(sMinFieldName) ? GetColumnDefaultValue(ft) : table.Rows[0][sMinFieldName]; ds.ModifyDomain(rd); } else { cv = domain as ICodedValueDomain; cv.Description = description; int codecount = cv.CodeCount; for (int a = 0; a < codecount; a++) { cv.DeleteCode(cv.GetCodeValue(0)); } for (int l = 0; l < table.Rows.Count; l++) { if (table.Rows[l][sDscribFieldName].ToString() != "") { if (table.Rows[l].IsNull(sEmunFieldName)) { continue; } cv.AddCode(table.Rows[l][sEmunFieldName], table.Rows[l][sDscribFieldName].ToString()); } } ds.ModifyDomain(cv); } } } MessageBox.Show("保存成功!"); } catch (COMException comEx) { MessageBox.Show(comEx.Message); this.DialogResult = DialogResult.None; } catch (System.Exception ex) { MessageBox.Show(ex.Message); this.DialogResult = DialogResult.None; } finally { if (cv != null) { //Marshal.ReleaseComObject(cv); cv = null; } if (rd != null) { //Marshal.ReleaseComObject(rd); rd = null; } if (domain != null) { //Marshal.ReleaseComObject(domain); domain = null; } } }