Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 29th, 2003, 12:48 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default Dataset does not get sorted correctly

Hi there,

I have a small XML file that I want to read into a dataset. The XML file contains a sortorder node and I'd like to sort the data in the XML file using that node.

Funny thing is that the data in the DataSet does not get sorted correctly. Here's the XML file:

<?xml version="1.0" encoding="utf-8" ?>
<mylinks>
<mylink>
    <url>Item 1</url>
    <name>Name of Item 1</name>
    <sortorder>10</sortorder>
</mylink>
<mylink>
    <url>Item 2</url>
    <name>Name of Item 2</name>
    <sortorder>20</sortorder>
</mylink>
</mylinks>

and here's the VB code I am using:
Code:
Dim MyDataSet As New DataSet
MyDataSet.ReadXml("links.xml")

Dim MyDataView As DataView = MyDataSet.Tables(0).DefaultView
MyDataView.Sort = "sortorder DESC"
For Each MyDataRow As DataRow In MyDataView.Table.Rows
    MessageBox.Show("Item is " & MyDataRow(0))
Next
DataGrid1.DataSource = MyDataView
The weird thing is that the For Each loop does *not* return the items in the requested order. That is, Item 1 comes first, then Item 2.

However, the DataGrid *does* display the data in the correct order.

Is there anything I should do to retrieve the data from the DataSet in the right order using For Each or For? I know it's in there in the right order, because the DataGrid can display the data correctly....

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old December 29th, 2003, 03:50 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

I believe the 'Table' property of the DataView refers to the underlying data table, which is not affected by the the 'sort' property. I think you want to use a DataRowView as, e.g.:
Code:
    For Each MyDataRow As DataRowView In MyDataView
        MessageBox.Show("Item is " & CStr(MyDataRow.Item(0)))
    Next
which does iterate in the correct sort order.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old December 29th, 2003, 04:49 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Jeff,

Thanks a lot. That was exactly what I was looking for.

Cheers,


Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Position of sorted nodes tall rog XSLT 5 July 9th, 2007 05:59 PM
How to get least value numbers in a sorted array? ashokparchuri Other Programming Languages 3 December 5th, 2006 09:25 AM
Urgent Issue to be sorted out mike_remember ASP.NET 1.0 and 1.1 Professional 5 November 1st, 2006 10:52 AM
sorted table crmpicco Javascript How-To 0 March 17th, 2005 10:29 AM
Search in a sorted list yajleejnus Classic ASP Basics 0 June 11th, 2003 04:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.