Groups | Blog | Home
all groups > dotnet windows forms databinding > december 2006 >

dotnet windows forms databinding : How to clear old data from DataGridView and DataSet?


RobinS
12/30/2006 11:39:56 PM
As I posted in the other ng where you posted this question,
try DataSet1.Reset()

Robin S.
----------------------------------------
[quoted text, click to view]

David Mayerovitch
12/31/2006 1:27:05 AM
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. Sometimes no data appears under these headings after the
loading 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.

Neither did
DataSet1.Clear()

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

David Mayerovitch
12/31/2006 8:25:29 AM
Thanks, Robin - That's just what I needed!
David

[quoted text, click to view]

RobinS
12/31/2006 12:15:32 PM
You're welcome; I'm glad it helped!

Robin S.
----------------------
[quoted text, click to view]

AddThis Social Bookmark Button