Results 1 to 6 of 6

Thread: Sorting a datatable for REALS!

  1. #1

    Thread Starter
    Addicted Member silentthread's Avatar
    Join Date
    Jun 2006
    Location
    Miami, Florida
    Posts
    143

    Sorting a datatable for REALS!

    Sorting a datatable for REALS! Just like they said in the South Park show where Kenny goes to heaven with his playstation system. "This is for reals."


    Okay, folks here it is...

    I'm a fan of Response.Write, and the other day, I needed to response.write the contents of a datatable. Don't ask me why. Just know that I had alot of neat javascript and ajax goodies going on. So, it was easier for me to just response.write the whole thing. Then I found out that...

    misleading... :-( Code:
    1. mytable.DefaultView.Sort = "column asc"

    Only works when you bind it, example a datagrid. This is a terrible thing. I can't believe, I was mislead to believe that DefaultView.Sort would do the trick. Shame on you world. lol. :-)

    Here is a function I created that does real sorting on a datatable....



    good stuff man... Code:
    1. Public Function SortTable(ByRef mytable As DataTable, ByVal column As String)
    2.         'Sorting a table for reals! By SiLentThReaD, AKA Numbchucks.
    3.  
    4.         'create a blank table. tablecopy is the name
    5.         Dim tablecopy As New DataTable
    6.         'make an exact copy of the table you passed in by reference, and
    7.         'store it in your tablecopy table
    8.         tablecopy = mytable.Copy()
    9.         'clear the contents of the table you just passed in, because
    10.         'you will soon fill it with newly sorted rows
    11.         mytable.Clear()
    12.         'create a rows array
    13.         Dim foundRows() As DataRow
    14.         'assign the foundrows array the values returned
    15.         'from the select. In this case, I left the
    16.         'filterexpression blank, because I want to get
    17.         'everything. I all I need to change is the sort
    18.         'order.
    19.         foundRows = tablecopy.Select("", column + " asc")
    20.  
    21.         'iterate through each row and import them into
    22.         'the table that you passed in by reference
    23.         Dim dbrow As DataRow
    24.         For Each dbrow In foundRows
    25.             mytable.ImportRow(dbrow)
    26.         Next
    27.  
    28.         'finally clear the contents of the tablecopy you created.
    29.         tablecopy.Clear()
    30.  
    31.     End Function
    Watch media as you download it! Excellent tool!
    FREE CUBA!
    MyBlog
    If you feel my post has helped, please rate it.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,689

    Re: Sorting a datatable for REALS!

    This code is, I'm afraid, a waste of time. You don't need to sort the DataTable itself. The reason that sorting the DefaultView works when binding is that the data is taken from the DefaultView by the bound control. If you want to see the data sorted then you just need to get the data from the DefaultView too. Try this:
    vb.net Code:
    1. Dim table As New DataTable
    2.  
    3. table.Columns.Add("Value", GetType(String))
    4.  
    5. table.Rows.Add("C")
    6. table.Rows.Add("B")
    7. table.Rows.Add("D")
    8. table.Rows.Add("A")
    9.  
    10. For Each row As DataRow In table.Rows
    11.     MessageBox.Show(CStr(row("Value")), "Rows Before Sort")
    12. Next row
    13.  
    14. For Each row As DataRowView In table.DefaultView
    15.     MessageBox.Show(CStr(row("Value")), "DefaultView Before Sort")
    16. Next row
    17.  
    18. table.DefaultView.Sort = "Value ASC"
    19.  
    20. For Each row As DataRow In table.Rows
    21.     MessageBox.Show(CStr(row("Value")), "Rows After Sort")
    22. Next row
    23.  
    24. For Each row As DataRowView In table.DefaultView
    25.     MessageBox.Show(CStr(row("Value")), "DefaultView After Sort")
    26. Next row
    I wrote this in a WinForms project so you can use something other than MessageBox.Show if you like, but it shows that after setting the DefaultView.Sort property the Rows property still returns the data in the original order but the DefaultView returns it in sorted order. You just access the DataRowView objects from the DefaultView instead of the DataRow objects from the Rows collection. If you really need a DataRow for some reason then you can get it from the Row property of the corresponding DataRowView.
    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

  3. #3

    Thread Starter
    Addicted Member silentthread's Avatar
    Join Date
    Jun 2006
    Location
    Miami, Florida
    Posts
    143

    Re: Sorting a datatable for REALS!

    Dude, you are an animal. Thanks for sharing. I have given you reputation on this. Thanks again!
    Watch media as you download it! Excellent tool!
    FREE CUBA!
    MyBlog
    If you feel my post has helped, please rate it.

  4. #4
    Lively Member
    Join Date
    Feb 2012
    Posts
    81

    Re: Sorting a datatable for REALS!

    I am trying to sort a datatable before I write it out to a text file.
    Fld4 has these values before sort:
    0, 1, 5, 22, 31, 41, 53, 101, 119, 0, 163, 221, 0, 300, 312, 15

    After the sort its exactly the same.
    Any suggestions would be appreciated.
    Thanks
    LB
    Code:
     Dim table As New DataTable
    
            ' Create four typed columns in the DataTable.
            table.Columns.Add("fld1", GetType(String)) ' String up to name variable
            table.Columns.Add("fld2", GetType(String)) ' field name
            table.Columns.Add("fld3", GetType(String)) ' string after name to start index
            table.Columns.Add("fld4", GetType(String)) ' start index
            table.Columns.Add("fld5", GetType(String)) ' string after start index to field length
            table.Columns.Add("fld6", GetType(String)) ' field length
            table.Columns.Add("fld7", GetType(String)) ' end of string
    
          For Each row As DataRow In table.Rows
                MessageBox.Show(CStr(row("fld4")), "Rows Before Sort")
            Next row
    
            table.DefaultView.Sort = "fld4 ASC"
    
            For Each row As DataRow In table.Rows
                MessageBox.Show(CStr(row("fld4")), "Rows After Sort")
            Next row

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,669

    Re: Sorting a datatable for REALS!

    Did you read the posts in this thread? It was mentioned:
    If you want to see the data sorted then you just need to get the data from the DefaultView too.
    Which you're not doing in your code...

    there's even an example in the sample code:

    Code:
    For Each row As DataRowView In table.DefaultView
        MessageBox.Show(CStr(row("Value")), "DefaultView After Sort")
    Next row
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Lively Member
    Join Date
    Feb 2012
    Posts
    81

    Re: Sorting a datatable for REALS!

    Yea, I read all the posts in this thread, I was just not sure how it applied to what I was trying to do.
    Some of us are not experts like you, some of the obvious stuff that glares out at you does not always poke us in the eye.

    Sorry to WASTE your time.

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