Possible to cast a dataview to a dataset type?

todonnell69

Member
Joined
Jun 8, 2005
Messages
15
Programming Experience
5-10
I'm trying to set up and XmlDataDocument based on the contents of a datagrid, so I can use the .Writeto method to put the data into an xml file.
The XmlDataDocument constructor takes a dataset(Im using datagrid.datasource which works with a ds, but not a dv). Im using a dataview as the datasource of my grid.
Is it possible to convert the dataview to a dataset type to satisfy the xmlDataDocument's constructor?
Thanks for any help!
Tom

VB.NET:
Dim ds As DataSet = New DataSet
        Dim cmd As SqlCommand = cnn.CreateCommand
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT * FROM Customers"
        Dim da As SqlDataAdapter = New SqlDataAdapter
        da.SelectCommand = cmd
        da.Fill(ds, "Customers")

        Dim dv As DataView = New DataView(ds.Tables("Customers"))
        dv.RowFilter = "Country = '" & txtCountry.Text & "'"
        dv.Sort = "CompanyName ASC"
        dgCustomers.DataSource = dv

        Dim xdd As XmlDataDocument = New XmlDataDocument(dgCustomers.DataSource)

        Dim xtw As XmlTextWriter = New XmlTextWriter("c:\temp\Cust.xml", System.Text.Encoding.UTF8)
        xdd.WriteTo(xtw)
        xtw.Flush()
        xtw.Close()
 
Last edited by a moderator:
DataView has Table property which is the DataTable, also you can use WriteXml/ReadXml methods of DataTable.
 
Thanks, I tried out what you're saying about the table property and it works, but it returns the dataset pre filtered - Maybe I'm doing it incorrectly but I'd like to get just the ones the dataview pulls out of the dataset. Would this be possible?
Thanks again...
 
WriteXml is intended for persisting a datatable or dataset to disk. It it not intended that you selectively filter records out of the dataset and then write them to disk. That's a bit like saying "yes, MS Word will let me write whatever swear words into a document, but it filters them out when I save it.."

If you only want the entries from a certain country, ordered by some value, then I suggest you only select those into your dataset in the first place (and please, read the PQ link in my signature to see how to filter your database query)

Plus, think about the sense of downlaoding all 5 million customers from your DB into a dataset, jsut so you can save the 2 that happen to be polish? Naw.. you SELECT * FROM customers WHERE countryID = @countryID ORDER BY ID ASC (or whatever variation of parameter is for your chosen db)

Read the PQ link
 
Well, that's kinda like asking how to cast a double glazed window into a meadow full of pretty flowers and lovely cows. Just because one shows you a view of the other doesnt mean that it is capable of being converted into the other, or that there is any objectified equivalence between the two ;)
 
Back
Top