How to clear old data from DataGridView and DataSet? RRS feed

  • Question

  • As an exercise in learning Visual Basic 2005, I am putting together a simple XML editor.

    I place on the form the objects DataGridView1 and DataSet1.

    ' Read an XML file into DataSet1:

    ' Bind the DataGridView to the DataSet:
    DataGridView1.DataSource = DataSet1

    ' Having determined that the TableName of the
    ' DataTable I am interested in is "bird",
    ' make this the DataMember of the DataGridView:

    DataGridView1.DataMember = "bird"

    ' Set up autogeneration of columns:
    DataGridView1.AutoGenerateColumns = True

    And now the contents of the file appear in the grid for viewing and editing. OK so far.

    But now I want to load in a new file. When I load "MUSICIANS.XML", with its DataMember "musician", the existing data from "BIRDS.XML" is not overwritten. It either remains as it is within the grid and the MUSICIANS data fails to appear, or, depending on the structure of the new file, the new data may be appended to the existing data. The number of columns and their headings remain what they were instead of reflecting the structure of the new file. Sometimes the headings remain without any data in the rest of the grid.

    Obviously I need some way of re-initializing the DataSet or the DataGridView or both before loading a new file. I have been able to do this only by re-instantiating the DataSet on each pass:

    Dim DataSet1 As New DataSet

    This gets rid of all previous data and I can load different files in succession. But it's not a good solution because instantiating the DataSet within a Sub means, I assume, that it won't be visible to other routines that I'll need for saving data back to a file. And it's probably bad for other reasons too.

    I tried setting
    DataGridView1.DataSource = Nothing
    but this didn't work.

    Neither did


    So can anyone tell me how to re-initialize the DataSet and/or the
    DataGridView so that I can load new files cleanly? I couldn't find anything in the documentation. (If there is an explanation somewhere, please point me to it.)


    Sunday, December 31, 2006 6:16 AM

All replies

  • To wipe out your DataSet's data before re-loading the next XML file, use:


    Then try re-binding your DataGridView.

    Sunday, December 31, 2006 3:22 PM
  • Thanks, BonnieB. I tried  DataSet1.Tables.Clear()and got the following error:

    "Cannot remove a table that has existing relations."

    I did however find that this does the job:




    Monday, January 1, 2007 10:19 PM
  • But doesn't that also wipe out any relationships? You have to set it all up again? Or does that relationship information happen to be defined in the XML? If so, then that sounds like it solves your problem quite easily.
    Monday, January 1, 2007 11:04 PM
  • If there are any relationships defined in the XML, it's through no effort on my part! The files I'm working with are of this form:

    <?xml version="1.0" standalone="yes"?>

    I have not yet got to learning what "relationships" are in this context (other than the tree relationships that are defined in any XML file) but apparently DataSet.ReadXml() finds them and complains if I try DataSet.Tables.Clear(). DataSet.Reset() seems to clear everything out without complaint.



    Tuesday, January 2, 2007 3:52 AM