Exemple #1
0
 private void tabControl1_MouseDown(object sender, MouseEventArgs e)
 {
     Console.WriteLine("tabcontrol mouse down");
     if (e.Button == MouseButtons.Right)
     {
         for (int i = 0; i < tabControl1.TabCount; ++i)
         {
             if (tabControl1.GetTabRect(i).Contains(e.Location))
             {
                 //tabs.Controls[i]; // this is your tab
                 var tableName = tabControl1.Controls[i].Text;
                 ContextMenuPrompt.ShowTableContextMenu(tableName);
             }
         }
     }
 }
Exemple #2
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);
                    }
                }
            }
        }