none
Removing rows from a DataTable without affecting the database RRS feed

  • Question

  • Hello,

    As the title says, I'm looking for a way to remove rows from the DataTable without linking the operation to the database. I just want to clear some rows I'm not interested in working with anymore at this time.

    Thanks.

     

    Monday, April 25, 2011 12:14 PM

Answers

All replies

  • DataRow.Delete will simply mark the row for deletion and will remain until you .Update the DataSet.

    http://msdn.microsoft.com/en-us/library/system.data.datarow.delete.aspx

     

    I think what you are after is the .Remove to clear particular DataRow from the DataRowCollection.  This will completely clear the data from the DataTable I believe.

    http://msdn.microsoft.com/en-us/library/system.data.datarowcollection.remove.aspx


    James Crandall ~ http://javitechnologies.com Spatial Database Solutions
    Monday, April 25, 2011 3:23 PM
  • I read about Remove, the documentation says:

    Calling Remove is the same as calling Delete and then calling AcceptChanges.

    That means it will update the database (AcceptChanges), and that's exactly what I don't want.

    Any other ideas?

    Thanks.

     

    Monday, April 25, 2011 3:45 PM
  • I read about Remove, the documentation says:

    Calling Remove is the same as calling Delete and then calling AcceptChanges.

    That means it will update the database (AcceptChanges), and that's exactly what I don't want.

    Any other ideas?

    Thanks.

     


    Well, perhaps you can clarify what it is you are intending to do?  What do you mean exactly by wanting to "clear rows"?  Do you mean not have them show up in a DataGridView or something like that?

    I always try to only return the data that I am interested in from the database.  So, I pretty much would never have rows that I'd want to "clear".  I do filter some things so that a temp view of data is restricted to specific rows, but that's about it.  Again if I didn't want to present data to the user, then it gets excluded by the T-SQL I have written on the database.

     


    James Crandall ~ http://javitechnologies.com Spatial Database Solutions
    Monday, April 25, 2011 3:49 PM
  • "AcceptChanges" does NOT fire the Update method on a DataAdapter--quite the opposite. It basically says, "The changes made to the table are already committed." AcceptChanges is called behind the scenes after the Update method is called to change the state of the rows to "unchanged".

    __________________________________________________________________
    William Vaughn
    Mentor, Consultant, Trainer, MVP
    http://betav.com
    http://betav.com/blog/billva
    http://www.hitchhikerguides.net

    “Hitchhiker’s Guide to Visual Studio and SQL Server (7th Edition)”

    Please click the Mark as Answer button if a post solves your problem!

    • Marked as answer by OLAPWisdom Monday, April 25, 2011 9:35 PM
    Monday, April 25, 2011 5:47 PM
    Moderator
  • What Bill says is correct (of course! ). And besides, the .Remove() does not actually call the .AcceptChanges(). The point of the statement:

    Calling Remove is the same as calling Delete and then calling AcceptChanges.

    ... is simply that it's as if the removed DataRows never existed in the DataTable. If your DataSet/DataTable previously had *other* changes in it, then .HasChanges() will still be true. If the only change to the DataSet/DataTable was the removal of that one DataRow, then .HasChanges() will be false.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Sunday, May 1, 2011 12:08 AM