470,624 Members | 2,235 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,624 developers. It's quick & easy.

How to clear old data from a DataGridView and a DataSet?

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.

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.)

Dec 30 '06 #1
0 1617

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Chuck Gantz | last post: by
2 posts views Thread by Rick Shaw | last post: by
6 posts views Thread by alexia.bee | last post: by
1 post views Thread by weird0 | last post: by
4 posts views Thread by =?Utf-8?B?UmludSBHb3BhbGFrcmlzaG5hIFBpbGxhaQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.