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