0

Here's my source code http://pastebin.com/embed_js.php?i=5unrfdhC for multiple undo/redo

private void btn_Undo_Click(object sender, EventArgs e) //To Undo previous searched record
    {
        if (redoStack.Count == 0 || redoStack.NeedsItem(dataGridView1))
        {
            redoStack.Push(dataGridView1.Rows.Cast<DataGridViewRow>().Where(r => !r.IsNewRow).Select(r => r.Cells.Cast<DataGridViewCell>().Select(c => c.Value).ToArray()).ToArray());
        }
        object[][] rows = undoStack.Pop();
        while (rows.ItemEquals(dataGridView1.Rows.Cast<DataGridViewRow>().Where(r => !r.IsNewRow).ToArray()))
        {
            rows = undoStack.Pop();
        }
        ignore = true;
        dataGridView1.Rows.Clear();
        for (int x = 0; x <= rows.GetUpperBound(0); x++)
        {
            dataGridView1.Rows.Add(rows[x]);
        }
        ignore = false;
        btn_Undo.Enabled = undoStack.Count > 0;
        btn_Redo.Enabled = redoStack.Count > 0;
    }


    private void btn_Redo_Click_2(object sender, EventArgs e)
    {
        if (undoStack.Count == 0 || undoStack.NeedsItem(dataGridView1))
        {
            undoStack.Push(dataGridView1.Rows.Cast<DataGridViewRow>().Where(r => !r.IsNewRow).Select(r => r.Cells.Cast<DataGridViewCell>().Select(c => c.Value).ToArray()).ToArray());
        }
        object[][] rows = redoStack.Pop();
        while (rows.ItemEquals(dataGridView1.Rows.Cast<DataGridViewRow>().Where(r => !r.IsNewRow).ToArray()))
        {
            rows = redoStack.Pop();
        }
        ignore = true;
        dataGridView1.Rows.Clear();
        for (int x = 0; x <= rows.GetUpperBound(0); x++)
        {
            dataGridView1.Rows.Add(rows[x]);
        }
        ignore = false;
        btn_Redo.Enabled = redoStack.Count > 0;
        btn_Undo.Enabled = undoStack.Count > 0;



    }

The function is suppose to work like when I extract records , the data will appear in a data grid view table . I'm trying to apply this multiple undo redo function so user can undo the previous search if they don't want it any more . When I Run , object[][] rows is highlighted and it state that my stack is empty what should I do ?

  • Add your code to your question, not many are willing to click on random links – Sayse Apr 28 '14 at 08:24
  • Post you code here, tell us what are you trying to run exactly and give more information on the class properties (like `redoStack`). And as a side note, you should consider code reuse, it looks like the only difference is the Stack object. – etaiso Apr 28 '14 at 10:29
  • Ok I've edited it . Please take a look tq so much – user3555674 Apr 29 '14 at 01:26

0 Answers0