DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2004
    Location
    México DF
    Posts
    127

    VB .NET Datatable.DefaultView.Sort not working?

    Hi everybody!

    I am attempting to sort a datatable... with no success so far! I am enclosing a sample code. Can anyone see what I am doing wrong?

    TIA for your help!

    mc

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Try
                Dim dt As DataTable = New DataTable()
                Dim dr1 As DataRow, dr2 As DataRow, dr3 As DataRow
                Dim sMessage As String = ""
                Dim dv As DataView
                'define structure of datatable
                dt.Columns.Add("Student")
                dt.Columns.Add("Score")
                dt.Columns.Add("Exam date")
                'add data to datatable
                dr1 = dt.NewRow()
                dr1(0) = "Ana" : dr1(1) = 60 : dr1(2) = "01/12/2007"
                dt.Rows.Add(dr1)
                '
                dr2 = dt.NewRow()
                dr2(0) = "Berta" : dr2(1) = 70 : dr2(2) = "01/12/2007"
                dt.Rows.Add(dr2)
                '
                dr3 = dt.NewRow()
                dr3(0) = "Carmen" : dr3(1) = 80 : dr3(2) = "01/12/2007"
                dt.Rows.Add(dr3)
                '
                For i = 0 To dt.Rows.Count - 1
                    sMessage = sMessage & dt.Rows(i)(0).ToString & " -- "
                Next i
                MsgBox("Before sorting - reading datatable: " & sMessage)
                sMessage = ""
                'sort
                dt.DefaultView.Sort = "Student DESC"
                dv = dt.DefaultView
                For i = 0 To dv.Table.Rows.Count - 1
                    sMessage = sMessage & dv.Table.Rows(i)(0).ToString & " -- "
                Next i
                MsgBox("After sorting - reading defaultview.table: " & sMessage)
            Catch objEx As Exception
                MsgBox objEx.Message
            End Try
        End Sub

  2. #2
    Join Date
    Oct 2008
    Posts
    141
    hi there,

    dt is a datatable, and dv is a dataview.
    so, try this:

    Code:
    dv = dt.defaultview
    dv.sort = "Student DESC"
    later, you can do:

    Code:
    for each row as datarowview in dv
    
    'do your stuff
    
    next
    best regards,

    tonci.

  3. #3
    Join Date
    Dec 2004
    Location
    México DF
    Posts
    127
    Thank you for your reply, but it's not working either if you check my code I was using the defaultview of the datatable as well). Thanks a lot anyway!

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,651
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Similar Threads

  1. 32 vs. 64 and VB with .NET - help
    By jak67 in forum .NET
    Replies: 2
    Last Post: 02-03-2009, 09:12 AM
  2. Upgrading a VB 6.0 Project to VB .Net
    By RajaSingh R. in forum .NET
    Replies: 1
    Last Post: 05-01-2002, 05:48 PM
  3. Replies: 2
    Last Post: 03-28-2002, 11:19 PM
  4. .NET vs. Enterprise Java: Who's Got Better Security?
    By Glen Kunene in forum Talk to the Editors
    Replies: 17
    Last Post: 03-23-2002, 12:43 AM
  5. Replies: 0
    Last Post: 01-29-2001, 05:47 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
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links