private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) { if (!DatabaseFunct.loadingTable) { DatabaseFunct.ChangeMainTable(Program.mainForm.tabControl1.SelectedTab.Name); } }
private void hideUnhideColumnToolStripMenuItem(object sender, System.EventArgs e) { ToolStripMenuItem menuItemSender = sender as ToolStripMenuItem; DatabaseFunct.HideUnhideColumn(menuItemSender.Name, TableMainGridView); menuItemSender.Checked = !menuItemSender.Checked; hideUnhideColumnsToolStripMenuItem.ShowDropDown(); }
private void removeTableToolStripMenuItem_Click(object sender, EventArgs e) { string[] input = Prompt.ShowDialog("Table Name:", "Remove Table", true, false, null); if (input[0] == "T") { DatabaseFunct.RemoveTable(input[1]); } }
private static void renameTable(object sender, System.EventArgs e) { MenuItem senderItem = (MenuItem)sender; string tableName = (string)senderItem.Tag; string[] input = Prompt.ShowDialog("Enter New Table Name:", "Rename \"" + tableName + "\" Table", true, false, null); if (input[0] == "T") { DatabaseFunct.ChangeTableName(tableName, input[1]); } }
private static void deleteRow(object sender, System.EventArgs e) { var confirmResult = MessageBox.Show("Are you sure to delete row at index " + rowIndex + " ?", "Confirm:", MessageBoxButtons.YesNo); if (confirmResult == DialogResult.Yes) { DatabaseFunct.RemoveRow(DGV, rowIndex); Program.mainForm.RecenterSubTables(); } }
private static void deleteColumn(object sender, System.EventArgs e) { var confirmResult = MessageBox.Show("Are you sure to delete " + colName + " Column?", "Confirm:", MessageBoxButtons.YesNo); if (confirmResult == DialogResult.Yes) { DatabaseFunct.RemoveColumn(colName, DGV); Program.mainForm.RecenterSubTables(); } }
private static void renameColumn(object sender, System.EventArgs e) { MenuItem senderMI = sender as MenuItem; dynamic[] dat = senderMI.Tag as dynamic[]; DataGridView _DGV = dat[0]; string _colName = dat[1]; string[] input = Prompt.ShowDialog("Enter New Column Name:", "Rename \"" + _colName + "\" Column", true, false, null); if (input[0] == "T") { DatabaseFunct.ChangeColumnName(_colName, input[1], _DGV); } }
internal static dynamic ValidateCellInput(DataGridViewCellEventArgs e, DataGridView DGV) { string tableDir = DGV.Name; string tableKey = DatabaseFunct.ConvertDirToTableKey(tableDir); Dictionary <int, Dictionary <string, dynamic> > tableData = DatabaseFunct.GetTableDataFromDir(tableDir) as Dictionary <int, Dictionary <string, dynamic> >; dynamic value = DGV.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; string colName = DGV.Columns[e.ColumnIndex].Name; var colType = DatabaseFunct.currentData[tableKey][colName]; if (colType == "Primary Key") { int index = 0; //confirm that no other primary index exists with the same key foreach (KeyValuePair <int, Dictionary <string, dynamic> > entry in tableData) { if (entry.Value.ContainsKey(colName) && entry.Value[colName] == value) { //don't display if primary key is null if (value != null) { System.Windows.Forms.MessageBox.Show("Duplicate primary key \"" + value + "\" exists at row index " + index); } return(null); } index++; } } else if (colType == "Numerical") { double a; if (!double.TryParse(value, out a)) { return(null); } else { value = a; } } return(value); }
private void newColumnToolStripMenuItem_Click(object sender, EventArgs e) { //store scroll value for later //scrollValue = panel1.AutoScrollPosition; if (DatabaseFunct.selectedTable != "") { string[] input = Prompt.ShowDialog("Name Column And Select Type", "Create Column", true, true, ColumnTypes.Types.Keys.ToArray <string>()); if (input[0] == "T") { DatabaseFunct.AddColumn(input[1], input[2], false, Program.mainForm.TableMainGridView); } } else { System.Windows.Forms.MessageBox.Show("no table selected"); } }
private void newRowToolStripMenuItem_Click(object sender, EventArgs e) { if (DatabaseFunct.selectedTable != "") { if (Program.mainForm.TableMainGridView.Columns.Count > 0) { DatabaseFunct.AddRow(Program.mainForm.TableMainGridView, false, 0); } else { System.Windows.Forms.MessageBox.Show("add a column first"); } } else { System.Windows.Forms.MessageBox.Show("no table selected"); } }
private static void addColToDisablerArrEvent(object sender, System.EventArgs e) { //add or remove to Disabler Arr MenuItem senderItem = (MenuItem)sender; string tableKey = ((string[])senderItem.Tag)[1]; string selectedColKey1 = ((string[])senderItem.Tag)[0]; string selectedColKey2 = senderItem.Name; if (senderItem.Checked) { DatabaseFunct.addColToDisablerArr(tableKey, selectedColKey1, selectedColKey2); } else if (MessageBox.Show("This will make rows of columns \"" + selectedColKey1 + "\" and \"" + selectedColKey2 + "\" not allow eachother to contain data at the same time. For each row, data from the cell of the column mentioned second will be deleted if there is data in the first column's cell.\n Are you sure you want to initiate this?", "Are You Sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { DatabaseFunct.addColToDisablerArr(tableKey, selectedColKey1, selectedColKey2); } cMenu.Dispose(); }
public void subTableNewColumnButton_Click(object sender, EventArgs e) { Button senderButton = sender as Button; DataGridView senderDGV = senderButton.Parent as DataGridView; if (DatabaseFunct.selectedTable != "") { string[] input = Prompt.ShowDialog("Name Column And Select Type", "Create Column", true, true, ColumnTypes.Types.Keys.ToArray <string>()); if (input[0] == "T") { DatabaseFunct.AddColumn(input[1], input[2], false, senderDGV); } } else { System.Windows.Forms.MessageBox.Show("no table selected"); } }
public void subTableNewRowButton_Click(object sender, EventArgs e) { Button senderButton = sender as Button; DataGridView senderDGV = senderButton.Parent as DataGridView; if (DatabaseFunct.selectedTable != "") { if (senderDGV.Columns.Count > 0) { DatabaseFunct.AddRow(senderDGV, false, 0); } else { System.Windows.Forms.MessageBox.Show("add a column first"); } } else { System.Windows.Forms.MessageBox.Show("no table selected"); } }
public void TableMainGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) { DataGridView senderDGV = sender as DataGridView; string tableDir = senderDGV.Name; string tableKey = DatabaseFunct.ConvertDirToTableKey(tableDir); Dictionary <int, Dictionary <string, dynamic> > tableData = DatabaseFunct.GetTableDataFromDir(tableDir) as Dictionary <int, Dictionary <string, dynamic> >; if (e.RowIndex > -1 && e.ColumnIndex > -1 && !DatabaseFunct.loadingTable) { //validate input of column type and return dynamic type var: dynamic val = ColumnTypes.ValidateCellInput(e, senderDGV); string displayVal = Convert.ToString(val); //don't want to trigger CellValueChanged a second time for checkboxes if (!(val is bool)) { Console.WriteLine("rewriting cell display to... " + Convert.ToString(val)); //corrected string value is applied to textbox senderDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = displayVal; } //add data to database var colName = senderDGV.Columns[e.ColumnIndex].Name; var rowIndex = e.RowIndex; //apply to data tableData[rowIndex][colName] = val; Console.WriteLine("value of entry " + rowIndex + " of column \"" + colName + "\" changed to: " + displayVal); KeyValuePair <int, Dictionary <string, dynamic> > KVRow = new KeyValuePair <int, Dictionary <string, dynamic> >(rowIndex, tableData[rowIndex]); DatabaseFunct.UpdateStatusOfAllRowCellsInDisablerArrayOfCell(senderDGV, tableKey, KVRow, colName); } }
public void RecenterSubTables() { //Console.WriteLine(scrollValue.Y + " and then: "); void subFunctExpandParentLoop(DataGridView DGV) { DataGridView parentDGV = DGV.Parent as DataGridView; string[] TupleDataArr = DGV.Name.Split('/'); Console.WriteLine(DGV.Name + " is the directory being expanded"); int row = Convert.ToInt32(TupleDataArr.Last().Split(',')[0]); Console.WriteLine("expanding row: " + row.ToString()); int subDGVHeight = (int)GetDataGridViewHeightAtRow(DGV, -1); //test parentDGV.Rows[row].DividerHeight = subDGVHeight + subTableSpacing; parentDGV.Rows[row].Height = parentDGV.RowTemplate.Height + subDGVHeight + subTableSpacing; DGV.Height = subDGVHeight + 5; if (parentDGV != Program.mainForm.TableMainGridView) { subFunctExpandParentLoop(parentDGV); } } //Resize rows first foreach (KeyValuePair <Tuple <DataGridView, int>, Tuple <string, DataGridView> > openSubTable in Program.openSubTables) { DataGridView subDGV = openSubTable.Value.Item2; subFunctExpandParentLoop(subDGV); /*DataGridView parentDGV = openSubTable.Key.Item1; * int row = openSubTable.Key.Item2; * * int subDGVHeight = (int)GetDataGridViewHeightAtRow(subDGV, -1); * //test * parentDGV.Rows[row].DividerHeight = subDGVHeight + subTableSpacing; * parentDGV.Rows[row].Height = parentDGV.RowTemplate.Height + subDGVHeight + subTableSpacing; * * subDGV.Height = subDGVHeight+5;*/ } Program.mainForm.TableMainGridView.Height = (int)GetDataGridViewHeightAtRow(Program.mainForm.TableMainGridView, -1) + 5; Program.mainForm.TableMainGridView.Width = ClientRectangle.Width - (vScrollBar1.Visible ? vScrollBar1.Width : 0); //Then move tables into position foreach (KeyValuePair <Tuple <DataGridView, int>, Tuple <string, DataGridView> > openSubTable in Program.openSubTables) { DataGridView subDGV = openSubTable.Value.Item2; DataGridView parentDGV = openSubTable.Key.Item1; int row = openSubTable.Key.Item2; //get x value relative to table key depth int xOffset = DatabaseFunct.ConvertDirToTableKey(subDGV.Name).Split('/').Count() - 1; xOffset *= indentationValue; //place subtable below row relative to parent table //adjust relative to TableMainGridView scroll subDGV.Location = new Point(xOffset, (int)(row != 0 ? GetDataGridViewHeightAtRow(parentDGV, row - 1) : subDGV.ColumnHeadersHeight) + parentDGV.RowTemplate.Height);//- Program.mainForm.TableMainGridView.VerticalScrollingOffset); subDGV.Width = ClientRectangle.Width - (vScrollBar1.Visible ? vScrollBar1.Width : 0) - xOffset; } //setToLastStoredScrollValue(); }
private static void shiftRight(object sender, System.EventArgs e) { DatabaseFunct.ShiftColumn(colName, DGV, false); }
//add new disabler array connection between two columns internal static void addColToDisablerArr(string tableKey, string selectedColKey1, string selectedColKey2) { //is being removed bool isRemoveDisablerCondition = false; void addOrRemoveFromDisablerArray(string disablerColKey, string selectedColKey) { //remove if already added if (!DatabaseFunct.currentData[tableKey].ContainsKey(disablerColKey + ColumnDisablerArrayExt)) { currentData[tableKey][disablerColKey + ColumnDisablerArrayExt] = new List <string> { selectedColKey }; } else { if (currentData[tableKey][disablerColKey + ColumnDisablerArrayExt].Contains(selectedColKey)) { currentData[tableKey][disablerColKey + ColumnDisablerArrayExt].Remove(selectedColKey); if (currentData[tableKey][disablerColKey + ColumnDisablerArrayExt].Count == 0) { currentData[tableKey].Remove(disablerColKey + ColumnDisablerArrayExt); } isRemoveDisablerCondition = true; } else { currentData[tableKey][disablerColKey + ColumnDisablerArrayExt].Add(selectedColKey); } //Console.WriteLine(DatabaseFunct.currentData[tableKey][disablerColKey + DatabaseFunct.ColumnDisablerArrayExt].ToString()); } } addOrRemoveFromDisablerArray(selectedColKey1, selectedColKey2); // do the reverse where selected col key disables the disabler column key addOrRemoveFromDisablerArray(selectedColKey2, selectedColKey1); //-------------------------------------------------------------------------------------------------- //i need to reconstruct the DGV names from the TableDataWithRow //parallel list with TableDataWithRow that contains DGV key names of TableData if they appear in openDGVs List <string> DGVKeysList = new List <string>(); //Check along all rows at this table depth for this column for conflicting data and delete it List <Dictionary <int, Dictionary <string, dynamic> > > TableDataWithRow = GetAllTableDataAtTableLevel(tableKey, ref DGVKeysList); //how can i tell if a cell is in an open table List <DataGridView> openDGVs = GetAllOpenDGVsAtTableLevel(tableKey); if (!isRemoveDisablerCondition) { //then disable the cells where there is conflict int tableIndex = 0; foreach (Dictionary <int, Dictionary <string, dynamic> > Table in TableDataWithRow) { string DGVKeyOfTable = DGVKeysList[tableIndex]; DataGridView openDGVOfTable = null; //get openDGVOfTable foreach (DataGridView openDGV in openDGVs) { if (openDGV.Name == DGVKeyOfTable) { openDGVOfTable = openDGV; } } foreach (KeyValuePair <int, Dictionary <string, dynamic> > KVRow in Table) { //if the selected cell isn't void of data then disable the other column cell and vice versa void disableCol2IfCol1HasData(string Col1, string Col2) { Console.WriteLine("Row Dat: "); Console.WriteLine(KVRow.Value.ToString()); Console.WriteLine("DoesDataRowCellContainData Input: "); if (KVRow.Value[Col1] != null) { Console.WriteLine(KVRow.Value[Col1].ToString()); } else { Console.WriteLine("null"); } Console.WriteLine("Erase/Disable Subject: "); if (KVRow.Value[Col2] != null) { Console.WriteLine(KVRow.Value[Col2].ToString()); } else { Console.WriteLine("null"); } if (DatabaseFunct.DoesDataRowCellContainData(KVRow, Col1)) { Type valType; if (KVRow.Value[Col2] != null) { valType = KVRow.Value[Col2].GetType(); } else { //val is null and null doesn't have a type so it will catch valType = null; } //erase value in data //if subtable if (valType == typeof(Dictionary <int, Dictionary <string, dynamic> >)) { KVRow.Value[Col2].Clear(); //close the subtable if subtable open if (openDGVOfTable != null) { Tuple <DataGridView, int> openSubTableKey = new Tuple <DataGridView, int>(openDGVOfTable, KVRow.Key); //open subtable exists if (Program.openSubTables.ContainsKey(openSubTableKey)) { //and that open subtable is of selectedColKey if (Program.openSubTables[openSubTableKey].Item2.Name.EndsWith("," + Col2)) { openSubTableKey.Item1.Controls.Remove(Program.openSubTables[openSubTableKey].Item2); //close subtable RemoveSubtableFromOpenSubtables(openSubTableKey); //close row openSubTableKey.Item1.Rows[openSubTableKey.Item2].Height = openSubTableKey.Item1.RowTemplate.Height; openSubTableKey.Item1.Rows[openSubTableKey.Item2].DividerHeight = 0; } } } } //if bool else if (valType == typeof(bool)) { KVRow.Value[Col2] = false; } //if not subtable or a bool else { KVRow.Value[Col2] = null; } //disable if dgv table is open if (openDGVOfTable != null) { DisableCellAtColAndRow(openDGVOfTable, Col2, KVRow.Key); } } } disableCol2IfCol1HasData(selectedColKey1, selectedColKey2); disableCol2IfCol1HasData(selectedColKey2, selectedColKey1); } tableIndex += 1; } } else //check what cells need to be re-enabled upon this condition being lifted { int tableIndex = 0; foreach (Dictionary <int, Dictionary <string, dynamic> > Table in TableDataWithRow) { string DGVKeyOfTable = DGVKeysList[tableIndex]; DataGridView openDGVOfTable = null; //get openDGVOfTable foreach (DataGridView openDGV in openDGVs) { if (openDGV.Name == DGVKeyOfTable) { openDGVOfTable = openDGV; } } foreach (KeyValuePair <int, Dictionary <string, dynamic> > KVRow in Table) { if (openDGVOfTable != null) { bool isDisabled1 = IsDataRowCellStillDisabled(tableKey, KVRow, selectedColKey1); bool isDisabled2 = IsDataRowCellStillDisabled(tableKey, KVRow, selectedColKey2); if (!isDisabled1) { EnableCellAtColAndRow(openDGVOfTable, selectedColKey1, KVRow.Key); } if (!isDisabled2) { EnableCellAtColAndRow(openDGVOfTable, selectedColKey2, KVRow.Key); } } } tableIndex += 1; } } }
private static void shiftDown(object sender, System.EventArgs e) { DatabaseFunct.ShiftRow(DGV, rowIndex, false); Program.mainForm.RecenterSubTables(); cMenu.Dispose(); }
private static void insertRow(object sender, System.EventArgs e) { DatabaseFunct.AddRow(DGV, true, rowIndex); Program.mainForm.RecenterSubTables(); cMenu.Dispose(); }
public static void ShowColumnDisablerContextMenu(DataGridView _DGV, string _colName) { cMenu.MenuItems.Clear(); DGV = _DGV; colName = _colName; List <MenuItem> optionsList = new List <MenuItem>(); MenuItem[] options = { }; int i = 0; string tableKey = DatabaseFunct.ConvertDirToTableKey(_DGV.Name); string[] tagData = new string[] { _colName, tableKey }; foreach (DataGridViewColumn col in _DGV.Columns) { if (col.Name != _colName) { //show if this column is already within the disabler bool isInDisablerArr = false; if (DatabaseFunct.currentData[tableKey].ContainsKey(_colName + DatabaseFunct.ColumnDisablerArrayExt)) { if (((List <string>)DatabaseFunct.currentData[tableKey][_colName + DatabaseFunct.ColumnDisablerArrayExt]).Contains(col.Name)) { isInDisablerArr = true; } else { Console.WriteLine(_colName + DatabaseFunct.ColumnDisablerArrayExt + " of key " + tableKey + " Does not contain: " + col.Name); } } else { Console.WriteLine(_colName + DatabaseFunct.ColumnDisablerArrayExt + " Does not exist within tablekey: " + tableKey); } string selectedColumnKey = col.Name; string selectedColumnText = col.HeaderText; optionsList.Add(new MenuItem() { Name = selectedColumnKey, Text = selectedColumnText, Tag = tagData, Checked = isInDisablerArr }); //assign event to each option optionsList[i].Click += new System.EventHandler(addColToDisablerArrEvent); i += 1; } } options = optionsList.ToArray(); cMenu.MenuItems.AddRange(options); Rectangle screenRectangle = Program.mainForm.RectangleToScreen(Program.mainForm.ClientRectangle); int titleHeight = screenRectangle.Top - Program.mainForm.Top; cMenu.Show(Program.mainForm, new Point(System.Windows.Forms.Cursor.Position.X - Program.mainForm.Location.X, System.Windows.Forms.Cursor.Position.Y - Program.mainForm.Location.Y - titleHeight)); }
internal static void ImportMDBFile(bool isReplace) { DatabaseFunct.loadingTable = true; string js = ""; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "mdb files (*.mdb)|*.mdb"; openFileDialog1.FilterIndex = 0; //openFileDialog1.FileName = fileName; if (openFileDialog1.ShowDialog() == DialogResult.OK) { if (isReplace) { DatabaseFunct.currentData = new SortedDictionary <string, dynamic>() { }; DatabaseFunct.ClearMainTable(); Program.mainForm.tabControl1.TabPages.Clear(); } if (openFileDialog1.FileName != "" && openFileDialog1.FileName.EndsWith(".mdb")) { defaultPath = openFileDialog1.FileName; js = File.ReadAllText(openFileDialog1.FileName); Console.WriteLine(js); SortedDictionary <string, dynamic> cd; cd = Newtonsoft.Json.JsonConvert.DeserializeObject <SortedDictionary <string, dynamic> >(js); subFunct(0, cd); bool valid = true; //deserialize all dicts and values void subFunct(int tableLevel, dynamic currentTable) { Console.WriteLine("current level being converted: " + tableLevel); if (currentTable is SortedDictionary <string, dynamic> || currentTable is Dictionary <string, dynamic> ) { ConvertStringKeyDict(currentTable); } else if (currentTable is Dictionary <int, Dictionary <string, dynamic> > ) { int i = 0; foreach (Dictionary <string, dynamic> value in currentTable.Values) { Console.WriteLine("at rowIndex: " + i); ConvertStringKeyDict(value); i += 1; } } else { Console.WriteLine("unrecognized table type: " + currentTable.GetType().ToString()); valid = false; } void ConvertStringKeyDict(dynamic ct) { Dictionary <string, dynamic> tableLevelKVs = new Dictionary <string, dynamic>(); foreach (dynamic key in ct.Keys) { Console.WriteLine("key is: " + key); //Console.WriteLine("Value Type is: " + ct[key].GetType().ToString()); //remove old table if duplicate: if (tableLevel == 0 && DatabaseFunct.currentData.ContainsKey(key)) { DatabaseFunct.currentData.Remove(key); Console.WriteLine("removing old table: " + key); } if (ct[key] is Newtonsoft.Json.Linq.JArray) { if (key == DatabaseFunct.ColumnOrderRefrence || key.EndsWith(DatabaseFunct.ColumnDisablerArrayExt)) { //column order list tableLevelKVs[key] = Newtonsoft.Json.JsonConvert.DeserializeObject <List <string> >(ct[key].ToString()); } } else if (ct[key] is Newtonsoft.Json.Linq.JObject) { if (tableLevel < 1) { //table tableLevelKVs[key] = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(ct[key].ToString()); } else { //entryTable tableLevelKVs[key] = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <int, Dictionary <string, dynamic> > >(ct[key].ToString()); } } else if (ct[key] is Newtonsoft.Json.Linq.JValue) { tableLevelKVs[key] = Newtonsoft.Json.JsonConvert.DeserializeObject <dynamic>(ct[key].ToString()); } else if (ct[key] == null) { tableLevelKVs[key] = null; } else { valid = false; } } //apply to ct foreach (KeyValuePair <string, dynamic> KV in tableLevelKVs) { ct[KV.Key] = KV.Value; if (!(KV.Value is ValueType) && KV.Value != null) { subFunct(tableLevel + 1, ct[KV.Key]); } } } } if (valid) { DatabaseFunct.currentData = cd; Program.mainForm.tabControl1.TabPages.Clear(); //load tables string[] mainTableKeys = DatabaseFunct.GetMainTableKeys(); foreach (string mainTableKey in mainTableKeys) { Program.mainForm.tabControl1.TabPages.Add(mainTableKey, mainTableKey); //change color of tab (doesn't work) Program.mainForm.tabControl1.TabPages[Program.mainForm.tabControl1.TabPages.IndexOfKey(mainTableKey)].BackColor = ColorThemes.Themes[ColorThemes.currentTheme]["ElseFore"]; Program.mainForm.tabControl1.TabPages[Program.mainForm.tabControl1.TabPages.IndexOfKey(mainTableKey)].ForeColor = ColorThemes.Themes[ColorThemes.currentTheme]["ElseBack"]; } //change table DatabaseFunct.ChangeMainTable(Program.mainForm.tabControl1.TabPages[0].Name); Program.mainForm.label1.Visible = false; } else { System.Windows.Forms.MessageBox.Show("ReadError"); } } else { System.Windows.Forms.MessageBox.Show("no valid file selected!"); } } DatabaseFunct.loadingTable = false; }
public void TableMainGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { DataGridView senderDGV = sender as DataGridView; string tableDir = senderDGV.Name; string tableKey = DatabaseFunct.ConvertDirToTableKey(tableDir); Dictionary <int, Dictionary <string, dynamic> > tableData = DatabaseFunct.GetTableDataFromDir(tableDir) as Dictionary <int, Dictionary <string, dynamic> >; // Ignore clicks that are on an empty table or a num column if (e.RowIndex < 0) { return; } if (e.ColumnIndex < 0) { return; } //if it is a button cell var colType = DatabaseFunct.currentData[tableKey][senderDGV.Columns[e.ColumnIndex].Name]; bool isEnabled = false; var selcell = senderDGV.Rows[e.RowIndex].Cells[e.ColumnIndex]; if (selcell is DataGridViewButtonCell) { DataGridViewButtonCell bcell = (DataGridViewButtonCell)selcell; isEnabled = ((Dictionary <string, dynamic>)bcell.Tag)["Enabled"]; } else if (selcell is DataGridViewCheckBoxCell) { DataGridViewCheckBoxCell cbcell = (DataGridViewCheckBoxCell)selcell; isEnabled = ((Dictionary <string, dynamic>)cbcell.Tag)["Enabled"]; } if (isEnabled) { if (colType == "SubTable") { void colorTabOfOpenTable(int selectedColIndex) { foreach (DataGridViewCell cell in senderDGV.Rows[e.RowIndex].Cells) { if (cell is DataGridViewButtonCell) { DataGridViewButtonCell bCell = cell as DataGridViewButtonCell; if (cell.ColumnIndex == selectedColIndex) { //change cell to where color is visible //bCell.FlatStyle = FlatStyle.Popup; bCell.Style.BackColor = Color.LightGreen; bCell.Style.SelectionBackColor = Color.LightGreen; } else if (!bCell.ReadOnly)//change cell to default if not disabled { //bCell.FlatStyle = FlatStyle.Standard; //default style if ((double)e.RowIndex % 2 == 0) { bCell.Style.BackColor = senderDGV.RowsDefaultCellStyle.BackColor; } else { bCell.Style.BackColor = senderDGV.AlternatingRowsDefaultCellStyle.BackColor; } bCell.Style.SelectionBackColor = Color.LightCyan; } } } } DatabaseFunct.loadingTable = true; //store scroll value for later //scrollValue = panel1.AutoScrollPosition; Tuple <DataGridView, int> subTableKey = new Tuple <DataGridView, int>(senderDGV, e.RowIndex); //add or replace subtable below row if (!Program.openSubTables.ContainsKey(subTableKey) || Program.openSubTables[subTableKey].Item1 != senderDGV.Columns[e.ColumnIndex].Name) { if (Program.openSubTables.ContainsKey(subTableKey)) { DatabaseFunct.RemoveSubtableFromOpenSubtables(subTableKey); } DataGridView newDGV = Program.GetGridView(); //-------------------------------------------------setting edits----- newDGV.Dock = DockStyle.None; newDGV.Name = senderDGV.Name + "/" + e.RowIndex.ToString() + "," + senderDGV.Columns[e.ColumnIndex].Name; //------------------------------------------------------------------- Button[] newButtonArr = Program.GetSubTableButtons(); //add newdgv to parent senderDGV.Controls.Add(newDGV); DatabaseFunct.LoadTable(newDGV); //add menu strip to new dgv foreach (Button b in newButtonArr) { newDGV.Controls.Add(b); } //add to open subtables Program.openSubTables.Add(subTableKey, new Tuple <string, DataGridView>(senderDGV.Columns[e.ColumnIndex].Name, newDGV)); //change color colorTabOfOpenTable(e.ColumnIndex); } else // close table { Console.WriteLine("close subtable"); DatabaseFunct.RemoveSubtableFromOpenSubtables(subTableKey); senderDGV.Rows[e.RowIndex].Height = senderDGV.RowTemplate.Height; senderDGV.Rows[e.RowIndex].DividerHeight = 0; //change color of all to default colorTabOfOpenTable(-1); //update displayValue of button cell Dictionary <int, Dictionary <string, dynamic> > subtableData = tableData[e.RowIndex][senderDGV.Columns[e.ColumnIndex].Name]; selcell.Value = ColumnTypes.GetSubTableCellDisplay(subtableData, senderDGV.Columns[e.ColumnIndex].Name, tableKey); } RecenterSubTables(); DatabaseFunct.loadingTable = false; } else if (colType == "Bool") { //bools don't disable other cells since they have no null state senderDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Convert.ToBoolean(senderDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue); } } }
public void TableMainGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { //scroll value here is re-added after cellvaluechanged //store scroll value for later //scrollValue = panel1.AutoScrollPosition; DataGridView senderDGV = sender as DataGridView; if (e.Button == MouseButtons.Right) { if (e.RowIndex > -1) { ContextMenuPrompt.ShowRowContextMenu(senderDGV, e.RowIndex); } else if (e.RowIndex == -1 && e.ColumnIndex > -1) { ContextMenuPrompt.ShowColumnContextMenu(senderDGV, senderDGV.Columns[e.ColumnIndex].Name); } } else if (e.Button == MouseButtons.Left) { if (e.RowIndex < 0 || e.ColumnIndex < 0) { return; } string tableDir = senderDGV.Name; string tableKey = DatabaseFunct.ConvertDirToTableKey(tableDir); Dictionary <int, Dictionary <string, dynamic> > tableData = DatabaseFunct.GetTableDataFromDir(tableDir) as Dictionary <int, Dictionary <string, dynamic> >; var colType = DatabaseFunct.currentData[tableKey][senderDGV.Columns[e.ColumnIndex].Name]; if (colType == "Foreign Key Refrence") { string error = ""; string refrencedTableKey = DatabaseFunct.currentData[tableKey][senderDGV.Columns[e.ColumnIndex].Name + DatabaseFunct.RefrenceColumnKeyExt]; //make sure table still exists and that table still has a primary key col if (!DatabaseFunct.currentData.ContainsKey(refrencedTableKey)) { error += "The table being refrenced, \"" + refrencedTableKey + "\" no longer exists. "; } else if (!DatabaseFunct.currentData[refrencedTableKey].ContainsValue("Primary Key")) { error += "The table being refrenced, \"" + refrencedTableKey + "\", no longer contains a primary key columnn, add one or delete this column."; } if (error == "") { //refrenced tables are always main tables var refrencedTable = DatabaseFunct.currentData[DatabaseFunct.currentData[tableKey][senderDGV.Columns[e.ColumnIndex].Name + DatabaseFunct.RefrenceColumnKeyExt]]; string primaryKeyCol = ""; //find primary key column name foreach (KeyValuePair <string, dynamic> KV in refrencedTable) { if (KV.Value is string && KV.Value == "Primary Key") { primaryKeyCol = KV.Key; } } if (primaryKeyCol != "") { List <string> primaryKeys = new List <string>(); primaryKeys.Add(""); foreach (KeyValuePair <int, Dictionary <string, dynamic> > entry in refrencedTable[DatabaseFunct.RowEntryRefrence]) { if (entry.Value[primaryKeyCol] != null) { primaryKeys.Add(entry.Value[primaryKeyCol]); } } //clear value if invalid if (!primaryKeys.Contains(senderDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].Value)) { senderDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = null; } var prevSel = senderDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; DataGridViewComboBoxCell cell = senderDGV.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell; cell.Items.Clear(); cell.Items.AddRange(primaryKeys.ToArray()); cell.Value = prevSel; } } else { senderDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = null; System.Windows.Forms.MessageBox.Show(error); } } else if (colType == "Parent Subtable Foreign Key Refrence") { string error = ""; string regex = "/(?!.*/).*"; string refrencedSubTableKey = DatabaseFunct.currentData[tableKey][senderDGV.Columns[e.ColumnIndex].Name + DatabaseFunct.ParentSubTableRefrenceColumnKeyExt]; string tableKeyContainingRefrencedSubTable = Regex.Replace(refrencedSubTableKey, regex, ""); string refrencedSubTableCol = Regex.Matches(refrencedSubTableKey, regex)[0].ToString().TrimStart('/'); string rowIndexOfRefrencedSubTable = ""; string subjectDirectory = tableDir.Clone().ToString(); //remove from subject until dir is equivalent to tableKeyContainingRefrencedSubTable while (DatabaseFunct.ConvertDirToTableKey(subjectDirectory) != tableKeyContainingRefrencedSubTable) { if (!subjectDirectory.Contains("/")) { throw new Exception("subject directory not found"); } //i also need to get the index for the row of this subtable //get after '/' regex = @"([^\/]+$)"; string output = Regex.Matches(subjectDirectory, regex)[0].ToString(); //get before ',' regex = @"^[^,]+"; rowIndexOfRefrencedSubTable = Regex.Matches(output, regex)[0].ToString(); // remove past and including last '/' regex = "/(?!.*/).*"; subjectDirectory = Regex.Replace(subjectDirectory, regex, ""); } //make sure table still exists and that table still has a primary key col if (!DatabaseFunct.currentData[tableKeyContainingRefrencedSubTable].ContainsKey(refrencedSubTableCol)) { error += "The subtable column being refrenced, \"" + refrencedSubTableCol + "\" within \"" + tableKeyContainingRefrencedSubTable + " no longer exists."; } else if (!DatabaseFunct.currentData[refrencedSubTableKey].ContainsValue("Primary Key")) { error += "The subtable being refrenced, \"" + refrencedSubTableKey + "\", no longer contains a primary key columnn, add one or delete this column."; } if (error == "") { var refrencedSubTable = DatabaseFunct.currentData[refrencedSubTableKey]; string primaryKeyCol = ""; //find primary key column name foreach (KeyValuePair <string, dynamic> KV in refrencedSubTable) { if (KV.Value is string && KV.Value == "Primary Key") { primaryKeyCol = KV.Key; } } if (primaryKeyCol != "") { // append the missing directory string of the same row and get the data Dictionary <int, Dictionary <string, dynamic> > refrencedTableData = DatabaseFunct.GetTableDataFromDir(subjectDirectory + "/" + rowIndexOfRefrencedSubTable + "," + refrencedSubTableCol) as Dictionary <int, Dictionary <string, dynamic> >; List <string> primaryKeys = new List <string>(); primaryKeys.Add(""); foreach (KeyValuePair <int, Dictionary <string, dynamic> > entry in refrencedTableData) { if (entry.Value[primaryKeyCol] != null) { primaryKeys.Add(entry.Value[primaryKeyCol]); } } //clear value if invalid if (!primaryKeys.Contains(senderDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].Value)) { senderDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = null; } var prevSel = senderDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; DataGridViewComboBoxCell cell = senderDGV.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell; cell.Items.Clear(); cell.Items.AddRange(primaryKeys.ToArray()); cell.Value = prevSel; } } else { senderDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = null; System.Windows.Forms.MessageBox.Show(error); } } } }