Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Sorting a dataset
Message
De
17/07/2013 18:05:50
 
 
À
17/07/2013 14:51:34
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01578641
Message ID:
01578668
Vues:
52
>I have this weird situation that I just found and I would like to know if some of you knows something about it.
>
>In my data class, since five years, I have the ability to define a sort field on a dataview. Then, I can scan the dataset rows and they appear in the order of the sort. This has worked well so far but they all have one thing in common. Wherever I was using that, so far, in several applications, it was always on a dataset that I created all the rows manually. Those dataset were not created from a SQL select. So, doing this:
>
>
>        Public oDataSet As DataSet = New DataSet
>        Private oDataView As DataView = New DataView
>
>            oDataSet.Tables.Add("Temp")
>
>            oDataSet.Tables(0).Columns.Add("Extension", GetType(System.String))
>            oDataSet.Tables(0).Columns.Add("LastUpdate", GetType(System.DateTime))
>            oDataSet.Tables(0).Columns.Add("Name", GetType(System.String))
>            oDataSet.Tables(0).Columns.Add("Size", GetType(System.Int32))
>            oDataSet.Tables(0).Columns.Add("JustName", GetType(System.String))
>            oDataSet.Tables(0).Columns.Add("CreationTime", GetType(System.DateTime))
>
>            ' Create the view
>            oDataView = oDataSet.Tables(0).DefaultView
>
>            ' Sort the dataset into the primary key
>            oData.oDataView.Sort = "AI"
>
>            ' For each record
>            For lnCounter = 0 To oData.nCount - 1
>                loRow = oData.oRows(lnCounter)
>
>            Next
>
>
>...is working ok.
>
>But, if I create the dataset from a SQL select such as:
>
>
>                Using loSQLConnection As New SqlConnection(oApp.aConnection(nConnectionString, 1))
>                     oCommand.Connection = loSQLConnection
>                     oDataAdapter.Fill(oDataSet)
>                End Using
>
>                ' Adjust to our default name
>                oDataSet.Tables("Table").TableName = "Temp"
>
>                ' Record count
>                nCount = oDataSet.Tables("Temp").Rows.Count
>
>                oDataView = New DataView
>
>                ' If we have no record
>                If nCount = 0 Then
>                    oRows = Nothing
>                Else
>                    oDataView = oDataSet.Tables("Temp").DefaultView
>                    oRows = oDataSet.Tables("Temp").Rows()
>                End If
>
>                ' Sort the dataset into the primary key
>                oData.oDataView.Sort = "AI"
> 
>                ' For each record
>                For lnCounter = 0 To oData.nCount - 1
>                    loRow = oData.oRows(lnCounter)
>
>                Next
>
>
>...this will not work. I would have to do this in order for the sort to work:
>
>
>        For Each loRowView In oData.oDataView
>        Next
>
>
>Anyone knows why I am forced to use the oData.oDataView approach to have my For Next to scan the records in the sort order when this is build from a SQL select and can obtain the sorting directly from the oDataSet.oRows when I create the dataset manually?

1) It helps if you indicate why it isn't working. What sort of error are you getting.
2) Why don't you want to use a For Each? Unless you are adding or removing from the collection, or need the index for some reason, it will prevent IndexOutOfRange errors and populate your loRow variable automatically.
3) Your first example doesn't show where oRows is being populated. Your second example shows it coming from the table. If you are going off the table, it is probably in the same order it was retrieved from the database. You should be looping through your DataView. If your first example was in order and also going off of the datatable, it is probably a coincidence.
4) If you need to access the DataRow, instead of using the DataRowView, you can do so through the DataRowView.Row property (http://msdn.microsoft.com/en-us/library/system.data.datarowview.row.aspx).
5) You may have better performance using LINQ instead of the DataView: http://www.adathedev.co.uk/2010/02/sorting-datatable-linq-performance.html.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform