Results 1 to 6 of 6

Thread: [RESOLVED] DataTable Sorting

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Resolved [RESOLVED] DataTable Sorting

    Hi All,

    I am creating a Datatable in memory from several sources. and need to order the data after I've build the Datatable before using it.

    I need to order the table by 4 columns and thought that by doing :

    Code:
    resultTable.DefaultView.Sort = "Column1, Column2, Column3, Column4";
    Would do the trick, it appears not to order them, but I do nto receive an error either.

  2. #2
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: [RESOLVED] DataTable Sorting

    Is this resolved ? if so please share your solution for others to use
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  3. #3
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: [RESOLVED] DataTable Sorting

    What he posted should have worked just fine.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

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

    Re: [RESOLVED] DataTable Sorting

    My guess is that the OP was expecting that code to sort the actual DataTable, as opposed to the DefaultView. If the DataTable is bound then the control(s) will display the sorted data because it comes from the DefaultView when binding. In code, you'd have to actually loop through the DefaultView rather than the DataTable itself.
    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

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Re: [RESOLVED] DataTable Sorting

    as Lord_rat said, this does work, my problem was that the sort is only set against the DefaultView NOT the underlying dataset that I actually want to work with, to work behind the scenes with a sorted resulttable I needed to do:

    Code:
    DataRow[] SortedRow = resultTable.Select(null, "Col1, col2, col3, col4, ASC", DataViewRowState.CurrentRows);
    The key here is passing null as the first parameter, it means I keep all my data. I then did the following to get the data back into my dataset:

    Code:
    for (int i = 0; i < SortedRow.Count(); i++)
                {
                    tempTable.ImportRow(SortedRow[i]);
                }

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

    Re: [RESOLVED] DataTable Sorting

    Quote Originally Posted by Bill Crawley View Post
    to work behind the scenes with a sorted resulttable I needed to do:
    No you didn't. You've already got a sorted result set. You just sorted the DefaultView so, if you want to access the sorted data, you do so using the DefaultView.
    vb.net Code:
    1. For Each row As DataRowView In myDataTable.DefaultView
    2.     MessageBox.Show(row("SomeColumn").ToString())
    3. Next
    If you really want a DataTable for some reason then the DefaultView, which is a DataView, has a method for that:
    vb.net Code:
    1. Dim newTable As DataTable = oldTable.DefaultView.ToTable()
    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

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