public static void Clear(DataGridView dataGridView)
        {
            if (dataGridView.TopLeftHeaderCell.Tag == null)
            {
                dataGridView.TopLeftHeaderCell.Tag = new DataGridViewGenericData();
            }
            if (dataGridView.TopLeftHeaderCell.Tag.GetType() != typeof(DataGridViewGenericData))
            {
                return;
            }

            DataGridViewGenericData dataGridViewGenericData = (DataGridViewGenericData)dataGridView.TopLeftHeaderCell.Tag;

            dataGridViewGenericData.RedoCellsStack.Clear();
            dataGridViewGenericData.UndoCellsStack.Clear();
        }
        public static void UndoDataGridView(DataGridView dataGridView)
        {
            if (IsDoingUndoRedo)
            {
                return;
            }
            if (dataGridView.CurrentCell == null)
            {
                return;
            }

            CellLocation currentCell = DataGridViewHandler.GetCurrentCellLocation(dataGridView.CurrentCell);

            if (dataGridView.TopLeftHeaderCell.Tag == null)
            {
                return;
            }
            if (dataGridView.TopLeftHeaderCell.Tag.GetType() != typeof(DataGridViewGenericData))
            {
                return;
            }

            DataGridViewGenericData undoAndRedo = (DataGridViewGenericData)dataGridView.TopLeftHeaderCell.Tag;

            if (undoAndRedo.UndoCellsStack.Count > 0)
            {
                Dictionary <CellLocation, DataGridViewGenericCell> undoCells = undoAndRedo.UndoCellsStack.Pop();

                Dictionary <CellLocation, DataGridViewGenericCell> redoCells = GetCellsFromStack(dataGridView, undoCells);
                PushToRedoStack(dataGridView, redoCells);

                IsDoingUndoRedo = true; //needs to be after PushToRedoStack or it will return

                NuberOfItemsToEdit = undoCells.Count;
                IsClipboardActive  = true;
                foreach (var cell in undoCells)
                {
                    DataGridViewHandler.SetCellDataGridViewGenericCell(dataGridView, cell.Key.ColumnIndex, cell.Key.RowIndex, cell.Value, true);
                }
                NuberOfItemsToEdit = 0;
                IsClipboardActive  = false;
            }

            DataGridViewHandler.SetCurrentCellLocation(dataGridView, currentCell);

            IsDoingUndoRedo = false;
        }
        public static bool CancelPushUndoStackIfNoChanges(DataGridView dataGridView)
        {
            if (IsDoingUndoRedo)
            {
                return(true);
            }
            if (dataGridView.TopLeftHeaderCell.Tag == null)
            {
                dataGridView.TopLeftHeaderCell.Tag = new DataGridViewGenericData();
            }
            if (dataGridView.TopLeftHeaderCell.Tag.GetType() != typeof(DataGridViewGenericData))
            {
                return(true);
            }

            DataGridViewGenericData undoAndRedo = (DataGridViewGenericData)dataGridView.TopLeftHeaderCell.Tag;
            Dictionary <CellLocation, DataGridViewGenericCell> peekCells = undoAndRedo.UndoCellsStack.Count == 0 ? null : undoAndRedo.UndoCellsStack.Peek();

            bool isEqual = true;

            if (peekCells == null)
            {
                isEqual = false;
            }

            if (isEqual)
            {
                foreach (CellLocation cellLocation in peekCells.Keys)
                {
                    DataGridViewGenericCell dataGridViewGenericCell =
                        DataGridViewHandler.CopyCellDataGridViewGenericCell(dataGridView, cellLocation.ColumnIndex, cellLocation.RowIndex);

                    if (peekCells[cellLocation] != dataGridViewGenericCell)
                    {
                        isEqual = false;
                        break;
                    }
                }
            }
            if (isEqual)
            {
                undoAndRedo.UndoCellsStack.Pop();
            }

            return(isEqual);
        }
        public static Dictionary <CellLocation, DataGridViewGenericCell> PeekUndoStack(DataGridView dataGridView)
        {
            if (IsDoingUndoRedo)
            {
                return(null);
            }
            if (dataGridView.TopLeftHeaderCell.Tag == null)
            {
                dataGridView.TopLeftHeaderCell.Tag = new DataGridViewGenericData();
            }
            if (dataGridView.TopLeftHeaderCell.Tag.GetType() != typeof(DataGridViewGenericData))
            {
                return(null);
            }

            DataGridViewGenericData undoAndRedo = (DataGridViewGenericData)dataGridView.TopLeftHeaderCell.Tag;

            return(undoAndRedo.UndoCellsStack.Count == 0 ? null : undoAndRedo.UndoCellsStack.Peek());
        }
        private static void PushToRedoStack(DataGridView dataGridView, Dictionary <CellLocation, DataGridViewGenericCell> cells)
        {
            if (IsDoingUndoRedo)
            {
                return;
            }
            if (dataGridView.TopLeftHeaderCell.Tag == null)
            {
                dataGridView.TopLeftHeaderCell.Tag = new DataGridViewGenericData();
            }
            if (dataGridView.TopLeftHeaderCell.Tag.GetType() != typeof(DataGridViewGenericData))
            {
                return;
            }

            DataGridViewGenericData undoAndRedo = (DataGridViewGenericData)dataGridView.TopLeftHeaderCell.Tag;

            undoAndRedo.RedoCellsStack.Push(cells);
        }
        public static void PushToUndoStack(DataGridView dataGridView, Dictionary <CellLocation, DataGridViewGenericCell> cells)
        {
            if (IsDoingUndoRedo)
            {
                return;
            }
            if (dataGridView.TopLeftHeaderCell.Tag == null)
            {
                dataGridView.TopLeftHeaderCell.Tag = new DataGridViewGenericData();
            }
            if (dataGridView.TopLeftHeaderCell.Tag.GetType() != typeof(DataGridViewGenericData))
            {
                return;
            }

            DataGridViewGenericData undoAndRedo = (DataGridViewGenericData)dataGridView.TopLeftHeaderCell.Tag;
            Dictionary <CellLocation, DataGridViewGenericCell> peekCells = undoAndRedo.UndoCellsStack.Count == 0 ? null : undoAndRedo.UndoCellsStack.Peek();

            undoAndRedo.UndoCellsStack.Push(cells);
        }