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 ?