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:
DataSet1.ReadXml("BIRDS.XML")
' 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.
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.
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.)
Thanks.
David