xml sorting/filtering

liquid8

Active member
Joined
Jan 14, 2003
Messages
31
I am currently reading data from an xml file into a dataset. In order to sort this data, would I just use a standard SQL statement or is there an easier way to sort/filter this data to return a sorted/filtered dataset? Could someone give a me quick example of using an SQL statement with a dataset if that is the way I should go about it?

Thanks,

LiQuiD8
 
There are basically two ways to view the data in a DataSet in a sorted order. You can use a DataView (including the one built into the DataTable), or you can use the Select method of the DataTable.

What are you doing with the DataSet? If youre binding, youll want a DataView. You can sort the tables built-in dataview with this:
myDataSet.Tables["Table1"].DefaultView.RowFilter = "FirstName like j*";

If you just need to loop through the rows in a sorted order to process them, use this:
C#:
DataRow[] rows = myDataSet.Tables["Table1"].Select("FirstName like j*";
foreach(DataRow row in rows)
    // Do something with row

-ner
 
The Select method looks like it will do great for what I need to do (just sort records alphabetically by a certain column, filter for specific records)

I am having some trouble with getting that to work, my data goes into the datarow array fine if i use no filter query, but i am getting errors otherwise.

The other problem I am having is putting the returned rows into a dataset. Shouldnt I be able to do something like this:

Dim newDataset as dataset
newDataset = myDataset.Clone

Dim rows as DataRow()
rows = myDataset.Tables("Table1").Select("","FirstName")
For each DataRow in rows
newDataset.Tables("Table1").Rows.Add(DataRow)
Next


Thanks for the help,

LiQuiD8
 
In your case, Id suggestion using the Copy method instead of Clone. Then just delete the ones you dont want.

I dont think you can use Rows.Add(...) with a row that belongs to another dataset. The Add method doesnt create a new instance of a row - it needs an existing instance. Youll have to use the datatables NewRow method to get a new instance of a datarow then populate that new row with data from the original datarow. I dont remember offhand if you can do that in one step or if you need to loop through every column and copy the contents. Hence my suggestion for a Copy and Delete instead :)

-Nerseus
 
I guess that would explain why the error i was getting was object ref. not set to an instance of an object... i couldnt find what object didnt have an instance.. so I guess it was like you said, the existing row couldnt be used, so that was a bit confusing!

woohoo! found what I needed though... the ImportRow method of a DataTable lets you import a copy of an existing row.

After that, all I had to do was this:

Dim DR As DataRow
For Each DR In NewRows
CurrentData.Tables(0).ImportRow(DR)
Next

thanks for the help!

LiQuiD8
 
Back
Top