Results 1 to 3 of 3

Thread: [RESOLVED] I'm having a sorting problem with dataSets. Please help me out..

  1. #1

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Resolved [RESOLVED] I'm having a sorting problem with dataSets. Please help me out..

    Hi,

    I want to sort my datatable column(0) alphabetically, but cant seem to find a way to do it.

    Just to give you an idea:
    vb.net Code:
    1. dim dsDATA as new dataset
    2. dsDATA.tables.add("TBL1")
    3. dsDATA.tables("TBL1").columns.add("NME")
    4. dsDATA.tables("TBL1").columns.add("SURNME")
    5. dsDATA.tables("TBL1").columns.add("NBR")
    6.  
    7. dsDATA.tables("TBL1").Rows.add("JACK","ABC","3242343")
    8. dsDATA.tables("TBL1").Rows.add("ABDUL","ASD","5324")
    9. dsDATA.tables("TBL1").Rows.add("ROD","BLACK","322468")
    Ok so my 1st column is the one with the heading "NME" (column 0)

    How do I sort this???

  2. #2

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: I'm having a sorting problem with dataSets. Please help me out..

    To elaborate, it's not really possible to sort an actual DataTable. When you bind a DataTable to controls, the data you see is actually the contents of the table's DefaultView, which is a DataView. You can sort and filter the data exposed by a DataView, but that doesn't actually change the DataTable at all.

    If all you're doing is binding then that's fine. If you need to access the data in code though, you must loop through the DataRowView's in the DataView rather than the DataRows in the DataTable. That's fine, because you can use those DataRowViews almost interchangeably with the DataRows. If you ever need a DataRow, you can get it from the Row property of the corresponding DataRowView.

    If you really do need a sorted DataTable then you can always set the Sort of the original table's DeafultView and then call its ToTable method. That will create a new DataTable that is in the same order as the DataView. You can also filter and project that way.

    The code provided in cicatrix's link will do the job, but it is a bit dodgy. First of all, the method is named AlphabeticSort when there's nothing in the code that limits it to sorting alphabetic text. Secondly, it creates a new DataSet that it never uses. Next, it uses a meaningless Integer for the sort direction when the .NET Framework has multiple enumerations for sort direction or you could declare your own. Finally, it returns dtTable.DefaultView.Table, which is exactly the same as just dtTable. Furthermore, there's no point returning the very same DataTable that was passed in because the caller already has a reference to it. I would change that code to this:
    vb.net Code:
    1. Private Sub SortDataTable(ByVal table As DataTable, _
    2.                           ByVal column As String, _
    3.                           ByVal direction As ListSortDirection)
    4.     table.DefaultView.Sort = String.Format("{0} {1}", _
    5.                                            column, _
    6.                                            direction)
    7. End Sub
    As you can see, the code is far simpler. It's also more generic as it lets you specify any column. You could even overload it to allow you to sort by multiple columns:
    vb.net Code:
    1. Private Sub SortDataTable(ByVal table As DataTable, _
    2.                           ByVal columns As String(), _
    3.                           ByVal directions As ListSortDirection())
    4.     If columns.Length <> directions.Length Then
    5.         Throw New ArgumentException("Column count must match direction count.")
    6.     End If
    7.  
    8.     Dim upperBound As Integer = columns.GetUpperBound(0)
    9.     Dim phrases(upperBound) As String
    10.  
    11.     For index = 0 To upperBound
    12.         phrases(index) = String.Format("{0} {1}", _
    13.                                        columns(index), _
    14.                                        directions(index))
    15.     Next
    16.  
    17.     table.DefaultView.Sort = String.Join(", ", phrases)
    18. End Sub
    Note that ListSortDirection is a member of the System.ComponentModel namespace. You might also use the System.Windows.Forms.SortOrder enumeration, which includes a None value. The code would need some slight adjustment to handle that though, e.g.
    vb.net Code:
    1. Private Sub SortDataTable(ByVal table As DataTable, _
    2.                           ByVal column As String, _
    3.                           Optional ByVal direction As SortOrder = SortOrder.None)
    4.     table.DefaultView.Sort = String.Format("{0} {1}", _
    5.                                            column, _
    6.                                            If(direction = SortOrder.None, _
    7.                                               String.Empty, _
    8.                                               direction.ToString))
    9. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width