views:

524

answers:

2

Hi everyone!

I have a problem with sorting a DataTable. I'm using DataView.Sort and then DataView.ToTable() to get the sorted table. Then I go through the table row by row and create nodes in a TreeView which is shown in a desktop application.

My problem is that I can sort the data in ascending order and it is shown in the TreeView in a correct way, but when I use the same sort mechanism but just change the order to descending it doesn't work.

But maybe the problem lies in the DataView.ToTable function. Does this keep a descending order or do I have to fix this myself when I create nodes in my TreeView by going through the DataRows in reverse order?

Hoping someone knows this.

/Ylva

A: 

Why you are not using DataView for iterate rows? From your question I do not see necessity in copying sorted content from one table to another table. Remember that data view is just sorted and filtered table representation.

So you should write something like this:

foreach (DataRow NextDataRow in DataView)

instead of

foreach (DataRow NextDataRow in DataTable.Rows)
arbiter
+1  A: 

I found the answer to my problem!

The problem was just that I did the sorting on two columns, like this: "ColumnA, ColumnB DESC" and I didn't realize until just now that the "DESC" part would only apply to the second column. -Silly of me.

But thanks for the reply anyway. I will use that approach when stepping through the DataView.

/Ylva