Results 1 to 5 of 5

Thread: Re-order a data table

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2005
    Posts
    18

    Question Re-order a data table

    Is there a way to sort a data table and keep the new sorted order permanent? I'm using the data table to populate a treeview, and the data is already in the table, but I want it sorted by a certain column. Can you help? Thanks!
    Last edited by arrowJ; Jul 14th, 2005 at 03:01 PM.

  2. #2
    Lively Member
    Join Date
    Jun 2004
    Posts
    99

    Re: Re-order a data table

    Try

    DataTable.DefaultView.Sort = "col1 ASC"

    you can use multiple columns and go ASC or DESC.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2005
    Posts
    18

    Re: Re-order a data table

    Thank you, that helps reorder the grid view, but I found out that it does not change the actual row order. For example, if I originally had this table:

    row# | id-field
    0 | 3
    1 | 2
    2 | 1

    and I use DataTable.DefaultView.Sort = "col1 ASC", the grid view will now show

    row# | id
    0 | 1
    1 | 2
    2 | 3

    but in the actual table, if I access row #0's id field, it will still be 3, not 1.


    VB Code:
    1. Console.WriteLine("BEFORE " & ds.Tables(0).Rows(2).Item("id"))
    2.             ds.Tables(0).DefaultView.Sort = "id DESC"
    3.             Console.WriteLine("AFTER " & ds.Tables(0).Rows(2).Item("id"))

    Output for both BEFORE and AFTER will be the same number. Is there anyway to make these sorted changes permanent, as in reassigns row#s. Thanks!!

  4. #4
    Member
    Join Date
    Jul 2004
    Posts
    35

    Re: Re-order a data table

    I'm having the exact same problem.

    Sorting my table visually works (it looks sorted) ... but when I try to access data from the newly sorted table, workrow = dtTable1.Rows(0) doesn't grab the 1st row in my newly sorted table ... it still grabs the old 1st row.

    I've spent hours trying to resolve this .... but with no luck.

    I think I've come to the conclusion that I need to use a DataView, but that wasn't working either.

    Can anyone shed some light?

    Thanks,
    beaker
    Last edited by beaker.net; Jul 30th, 2005 at 08:37 AM.

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

    Re: Re-order a data table

    It is not possible to change the order of rows in a DataTable without actually removing them and then re-adding in the order you desire. The DefaultView property of a DataTable is a DataView object. When you bind a control to a DataTable, it is actually the DefaultView that determines what is displayed. If you set the RowFilter, RowStateFilter or Sort property of the DefaultView, the actual DataTable itself is unaffected, but if you iterate over the rows in the DataView you will find that they reflect the change. Note that a DataView is similar to a DataRowCollection, which is the type of the Rows property of a DataTable. A DataView contains DataRowView objects, which are similar but not quite the same as DataRow objects.

    Edit:
    To sum this up, the rows of a DataTable's DefaultView property will always correspond to the data displayed in a bound DataGrid. If you sort the DataView, the change is reflected in the DataGrid. If you sort the DataGrid by clicking on a column header, the change is reflected in the DataView. The underlying DataTable is affected by edits, i.e. inserting, updating or deleting rows, but not by sorting or filtering.
    Last edited by jmcilhinney; Jul 30th, 2005 at 08:47 PM.
    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