private void editButton_Click(object sender, EventArgs e) { var tableReferences = CurrentTablePreference.TableReferences; if (tableReferences == null) return; var selectedItem = (string)tableRefList.SelectedItem; if(selectedItem == null) return; var tableReference = tableReferences[selectedItem]; if (tableReference == null) return; bool editState = tableReferenceGroup.Enabled; tableReferenceGroup.Enabled = !editState; if (tableReferenceGroup.Enabled) { editButton.Text = "Stop"; addReferenceButton.Enabled = editState; removeReferenceButton.Enabled = editState; } else { //set state control editButton.Text = "Edit"; addReferenceButton.Enabled = editState; removeReferenceButton.Enabled = editState; // update reference tableReference.ReferenceTable = (string) detailTableCombo.SelectedItem; refName.Text = tableReference.ReferenceTable; tableReference.Name = refName.Text; tableReference.ReferenceType = (ReferenceType)refTypeCombo.SelectedItem; AddColumnDetail(tableReference, refColumnGrid); if(!tableReferences.ContainsKey(tableReference.Name)) { tableReferences.Add(tableReference.Name, tableReference); tableReferences.Remove(selectedItem); } else { tableReferences[tableReference.Name] = tableReference; } PopularTableReferencesList(); if (tableReference.ReferenceType == ReferenceType.OneToMany) { // update to other table if reference is one to many ApplicationPreferences otherPref = GetPreference(_tablePreferences, tableReference.ReferenceTable); var otherTableRefs = otherPref.TableReferences; if (otherTableRefs != null && otherTableRefs.Count > 0) { foreach (KeyValuePair<string, TableReference> pair in otherTableRefs) { TableReference reference = pair.Value; if (reference.ReferenceTable.Equals(CurrentTablePreference.TableName) && reference.ReferenceType == ReferenceType.ManyToOne ) { return; } } } if (otherTableRefs == null) otherTableRefs = new Dictionary<string, TableReference>(); otherPref.TableReferences = otherTableRefs; TableReference inverseRef = new TableReference { Name = CurrentTablePreference.TableName, ReferenceTable = CurrentTablePreference.TableName, ReferenceType = ReferenceType.ManyToOne, TableColumns = new Dictionary<ColumnDetail, ColumnDetail>() }; AddColumnDetail(inverseRef, refColumnGrid); otherTableRefs.Add(inverseRef.Name, inverseRef); } } }
private void AddColumnDetail(TableReference tableReference, DataGridView refColumnGrid1) { tableReference.TableColumns.Clear(); foreach (DataGridViewRow row in refColumnGrid1.Rows) { var colName1 = row.Cells[0].Value; var colName2 = row.Cells[1].Value; if (colName1 == null || colName2 == null) continue; ColumnDetail _column = GetColumn(columnDetailBindingSource, colName1.ToString()); ColumnDetail _refColumn = GetColumn(refColumnDetailBindingSource, colName2.ToString()); if (_column == null || _refColumn == null) continue; tableReference.TableColumns.Add(_column, _refColumn); } }
private void addDetailTable_Click(object sender, EventArgs e) { if (tablesComboBox.SelectedIndex < 0) return; if (_tablePreferences.Count <= 0) return; var applicationPreferences = _tablePreferences[tablesComboBox.SelectedIndex]; if(applicationPreferences.TableReferences == null) { applicationPreferences.TableReferences = new Dictionary<string, TableReference>(); } TableReference reference = new TableReference { Name = "REF" + new Random(DateTime.Now.Millisecond).Next().ToString(), ReferenceType = (ReferenceType)refTypeCombo.SelectedItem, TableColumns = new Dictionary<ColumnDetail, ColumnDetail>() }; applicationPreferences.TableReferences.Add(reference.Name,reference); tableRefList.Items.Add(reference.Name); }