re-order a dataset

M

MattB

Hi, I have a dataset (vb.Net 1.1) that I need to go through and re-order
based on the data. I'm wondering if there is a nice relatively automated
way to do this, but I kind of doubt it. There is not currently a single
field to use in a DataView's order by parameter.
The data has two fields that decide the order, node_id and parent_id.
The parent_id is the node_id of the row above. If there is no
corresponding row to the parent_id then we have row 0.

I've messed around a little with looping through the table and I haven't
found any elegant (or even not ugly) solutions. Two options that come to
mind are cloning the table and copying the rows in order, probably
making multiple passes. Another would be to add an "order" column and
populating that as I loop through and then use a dataview to order the
rows according to that field.

Anyone got any better solutions? Could I create a recursive DataRelation
to do this? I'd love to hear any ideas!
Thanks!

Matt
 
H

Härry Marcel

Hi Matt

Why not create a view from it and setting its sort property to "node_id asc
parent_id asc" ?

Marcel
 
M

MattB

Because the node_id's are not in the order I need. They are in the order
created which doesn't work for this.

They need to be in the order: parent, <-- child of, <-- child of, etc...

Thanks!
 
M

MattB

Alright, I solved my problem. I could have used DataRelations or
DataViews and ended up using DataViews, just because that's what I was
trying when I solved it. Here's the code block in case anyone needs a
solution to a similar problem:

'set the correct order of IT nodes for display
dtNodes.Columns.Add("order", Type.GetType("System.Int16"),
Nothing)

Dim dv2 As New DataView(dtNodes, "order is null", Nothing,
DataViewRowState.CurrentRows)
Dim o As Int16

While dv2.Count > 0
For Each r In dtNodes.Rows
Dim dvP As New DataView(dtNodes, "node_id = " &
r("parent_id"), Nothing, DataViewRowState.CurrentRows)
If dvP.Count = 0 And r("order").GetType Is
Type.GetType("System.DBNull") Then
r("order") = o
o += 1
ElseIf dvP.Count > 0 Then
If Not dvP(0)("order").GetType Is
Type.GetType("System.DBNull") Then
If dvP(0)("node_id") = r("parent_id") And
r("order").GetType Is Type.GetType("System.DBNull") Then
r("order") = o
o += 1
End If
End If
End If

If dv2.Count = 0 Then Exit While
Next
End While
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top