Results 1 to 7 of 7

Thread: How to sort a datatable

  1. #1
    Join Date
    Jan 2010
    Posts
    44

    How to sort a datatable

    I am trying to sort on a datatable in vb.net 2003 but the sorting is not working. I use the following code "DTPROD.DefaultView.Sort = ("Animal, DPROD DESC")".

    When I view the information in a datagrid sorting is done. If I use a loop to check my column my datatable "Animal" is not sorted.

    Is there someone who can explain why I see the sorts in the datagrid although my loop on my datatable gives me no rows sorted?

  2. #2
    Join Date
    Nov 2008
    Posts
    1,185

    Re: How to sort a datatable

    I guess the property DefautView is just a sort of view when the data are displayed in a DataGrid. If you want your datatble sorted, you can not do it directly in your query, if as I suppose your data from a database?

  3. #3
    Join Date
    Jan 2010
    Posts
    44

    Re: How to sort a datatable

    Thank you very much for answering me. Actually I also believe that the sorting is only for display in a datagrid. My data does not come from a comic. It comes from an XML file. I transferred into the datatable, I do some manipulation before transferring everything into a comic. The key for me is being able to sort the data before making my manipulations.

  4. #4
    Join Date
    Nov 2008
    Posts
    1,185

    Re: How to sort a datatable

    So I guess you'll have to sort by manually. You use two DataTables and use String.Compare (to sort by alphabetical order)

  5. #5
    Join Date
    Jan 2010
    Posts
    44

    Re: How to sort a datatable

    Do you think I could send my data in more than a datatable to my sort? Is it that there would be another component that would allow me to make a sort of 2 columns? And who could then be transferred to a datatable.

    Example array, ArrayList, or another dataset?

  6. #6
    Join Date
    May 2008
    Posts
    685

    Re: How to sort a datatable

    You must use your defautview of dataTable and not directly
    Code:
    datatable.rows(index).item
    if my memory is correct it should be something like this:
    Code:
    datatable.defautview.item(index).item

  7. #7
    Join Date
    Nov 2008
    Posts
    1,221

    Re: How to sort a datatable

    You can always use the DefaultView as DataTable. One can modify your code:

    Code:
    DTPROD.DefaultView.Sort = "Animal, DPROD DESC"
    and then try to do this:

    Code:
    yourDataGrid.DataSource = DTPROD.DefaultView.ToTable();

Similar Threads

  1. Replies: 3
    Last Post: 04-01-2011, 01:25 AM
  2. Ado.Net: Reorder columns in a DataTable
    By Sergio 1 in forum Software Development
    Replies: 3
    Last Post: 29-12-2010, 02:45 AM
  3. How to use Bubble sort in C# to sort parallel arraylists
    By Ground 0 in forum Software Development
    Replies: 3
    Last Post: 03-08-2009, 12:12 PM
  4. Exporting datatable to excel with ADO.net
    By Sanket07 in forum Software Development
    Replies: 2
    Last Post: 03-02-2009, 05:52 PM
  5. How can i export datatable to excel
    By Leena in forum Software Development
    Replies: 3
    Last Post: 30-01-2009, 11:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,819,389.15558 seconds with 16 queries