datagridview的多次撤消/重做

时间:2014-04-28 08:18:47

标签: c# datagridview undo-redo

这是我的源代码http://pastebin.com/embed_js.php?i=5unrfdhC,用于多个撤消/重做

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;



    }

该函数假设像我提取记录时一样工作,数据将出现在数据网格视图表中。我正在尝试应用此多重撤消重做功能,以便用户可以撤消之前的搜索,如果他们不再需要它。 当我运行时,对象[] []行被突出显示并且它表明我的堆栈是空的我该怎么办?

0 个答案:

没有答案