Applying a filter but still keeping the natural row order

  • Thread starter Thread starter Andrew Backer
  • Start date Start date
A

Andrew Backer

Hope someone can help me with this, since I am stumped.

I need to apply a filter to a dataset so that I can clone those rows,
while keeping the resulting rows in the same natural order as the
original dataset. My code right now is something like this:

// get data, create clone of table struction in newDataSet
for each row as DataRow in table.Select( "name<>'Jimmy'")
newDataSet.Tables(0).ImportRow( row )
next

The results I get out of Select appear to be sorted starting with the
first column, which might be fine sometimes, but this particular
result set is sorted by the 2nd column only, and descending not
ascending.

I don't mind doing something that is a bit slower if It means that the
results will come out in the same order. I need to respect, as much
as possible, the way the stored procedure designer ordered these
things, and if I could dictate all the sorts from code (and it
probably should have been that way to begin with) then I would.

I have workarounds for the specific instances that are causing
problems, but I was hoping for a general solution.

Thanks,
//Andrew Backer
 
Andrew,

If you need a copy of a table without certain records specified in your
filter you could always do something similar to the following:
' first create a copy of the existing table
Dim newTable As DataTable = table.Copy()
' now remove the records that are specified in the filter:
Dim view As DataView = newTable.DefaultView
view.RowFilter = "name<>'Jimmy'" ' or any other filter expression
For Each row As DataRowView in view
row.Delete()
Next
newTable.AcceptChanges()

That should keep the original records order
 
Back
Top