Sorting a datatable for REALS!-VBForums
Results 1 to 3 of 3

Thread: Sorting a datatable for REALS!

Hybrid View

  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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    86,138

    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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    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.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.