Exemplo n.º 1
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);
                }
            }
        }