Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
            }
        }
Ejemplo n.º 3
0
        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);
                }
            }
        }
Ejemplo n.º 4
0
        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);
                    }
                }
            }
        }