none
How to store a DataRow information after deletion from a DataTable for further use? RRS feed

  • Question

  • I'm trying to implement undo redo in my DataGrid which is bound to a DataTable. On undo, the row has to be deleted and on redo, the row has to be added back to the DataTable. I know that once a row is deleted from a table it cannot be brought back. So I tried creating another table to store the row information. On undo, when I try to add the row to the new table, Argument Exception (This row already belongs to another table.) is thrown.

            DataTable table;
            DataTable tableRemoved = new DataTable();
            DataRow row;
            DataRow rowRemoved;
            public InsertRowCommand(DataTable dt, DataRow dr)
            {
                table = dt;
                row = dr;
                rowRemoved = dr;
            }

            public void Execute(object param)
            {
                bool redo = (bool)param;
                if (redo)
                {
                     //will implement later
                }
                else
                {
                    table.Rows.Remove(row);
                    tableRemoved.Rows.Add(rowRemoved); //Argument Exception thrown 
                }
            }


    • Edited by NandaSarathy Monday, May 25, 2015 6:11 AM
    • Moved by Tina-Shi Wednesday, June 3, 2015 5:01 AM the issue is not related to the VS general
    • Moved by Sheng Jiang 蒋晟 Friday, June 5, 2015 5:34 PM
    Monday, May 25, 2015 6:10 AM

All replies

  • You can do following:

    public InsertRowCommand(DataTable dt, DataRow dr)
            {

                table = dt;
                row = dr;

                rowRemoved =  tableRemoved.NewRow();             
                 rowRemoved.ItemArray = dr.ItemArray.Clone() as object[];
            }

     public void Execute(object param)
            {
                bool redo = (bool)param;
                if (redo)
                {
                     //will implement later
                }
                else
                {
                    table.Rows.Remove(row);

                    tableRemoved.ImportRow(rowRemoved); 
                 }
            }

    Monday, May 25, 2015 6:44 AM
  • Hi Beginner1793,

    Thank you for posting in MSDN forum.

    Since this forum is discuss Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor.

    As you said about how to store a DataRow information after deletion from a DataTable for further use issue, I think that it is out of support range of this forum.

    So to help you find the correct forum support this issue, could you please tell me which kind of project you create in the VS IDE?

    Is it related to the ASP.NET web project or other type project.

    If it is related to the ASP.NET web project, I suggest you could consult this issue directly to the ASP.NET forum:http://forums.asp.net/ , you will get better support from the ASP.NET experts.

    If no, please tell me more detailed message about this issue so that we will help you find the correct forum support this issue.

    Thanks for your understanding!

    Best Regards,

    Update:

    Since I know that the issue is related to the VS IDE, so I am moving your question to the moderator forum ("Where is the forum for..?"). The owner of the forum will direct you to a right forum.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Tina-Shi Wednesday, June 3, 2015 5:01 AM
    Tuesday, May 26, 2015 2:48 AM