PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2012 Sort DataTable or DataView by Custom column-VBForums
Results 1 to 5 of 5

Thread: Sort DataTable or DataView by Custom column

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,214

    Sort DataTable or DataView by Custom column

    I have a table that has a column that is a class. How can I sort on that column.

    The class implements IComaparable and IEquatable and does sort if stored in a list.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,746

    Re: Sort DataTable or DataView by Custom column

    I've never tried it but I would expect that, if your class implements IComparable, you would sort that column in exactly the same way as you would any other, e.g.
    vb.net Code:
    1. myDataTable.DefaultView.Sort = "MyColumn" 'Sort in default direction, which is ascending.
    2. myDataTable.DefaultView.Sort = "MyColumn ASC" 'Sort ascending.
    3. myDataTable.DefaultView.Sort = "MyColumn DESC" 'Sort descending.
    I would have expected that you would try that so I'm wondering whether you have an it didn't work.

  3. #3

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,214

    Re: Sort DataTable or DataView by Custom column

    Quote Originally Posted by jmcilhinney View Post
    I've never tried it but I would expect that, if your class implements IComparable, you would sort that column in exactly the same way as you would any other, e.g.
    vb.net Code:
    1. myDataTable.DefaultView.Sort = "MyColumn" 'Sort in default direction, which is ascending.
    2. myDataTable.DefaultView.Sort = "MyColumn ASC" 'Sort ascending.
    3. myDataTable.DefaultView.Sort = "MyColumn DESC" 'Sort descending.
    I would have expected that you would try that so I'm wondering whether you have an it didn't work.
    The Class does implement IComparable. Yes I did try that.

    If I add the class instances to a list I can sort the list by using the .Sort method of the list, and that works.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,746

    Re: Sort DataTable or DataView by Custom column

    I just did this in VS 2017:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private table As New DataTable
    4.  
    5.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    6.         With table.Columns
    7.             .Add("Name", GetType(String))
    8.             .Add("Number", GetType(Integer))
    9.             .Add("Thing", GetType(Thing))
    10.         End With
    11.  
    12.         With table.Rows
    13.             .Add("First", 1, New Thing With {.Name = "One"})
    14.             .Add("Second", 2, New Thing With {.Name = "Two"})
    15.             .Add("Third", 3, New Thing With {.Name = "Three"})
    16.             .Add("Fourth", 4, New Thing With {.Name = "Four"})
    17.             .Add("Fifth", 5, New Thing With {.Name = "Five"})
    18.             .Add("Sixth", 6, New Thing With {.Name = "Six"})
    19.             .Add("Seventh", 7, New Thing With {.Name = "Seven"})
    20.             .Add("Eighth", 8, New Thing With {.Name = "Eight"})
    21.             .Add("Ninth", 9, New Thing With {.Name = "Nine"})
    22.             .Add("Tenth", 10, New Thing With {.Name = "Ten"})
    23.         End With
    24.  
    25.         DataGridView1.DataSource = table
    26.     End Sub
    27.  
    28. End Class
    29.  
    30. Public Class Thing
    31.     Implements IComparable, IComparable(Of Thing), IEquatable(Of Thing)
    32.  
    33.     Public Property Name As String
    34.  
    35.     Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
    36.         Return CompareTo(DirectCast(obj, Thing))
    37.     End Function
    38.  
    39.     Public Function CompareTo(other As Thing) As Integer Implements IComparable(Of Thing).CompareTo
    40.         Return Name.CompareTo(other.Name)
    41.     End Function
    42.  
    43.     Public Overloads Function Equals(other As Thing) As Boolean Implements IEquatable(Of Thing).Equals
    44.         Return Name.Equals(other.Name)
    45.     End Function
    46.  
    47.     Public Overrides Function ToString() As String
    48.         Return Name
    49.     End Function
    50.  
    51. End Class
    When I ran the project, the grid displayed the three columns and I was able to click each column header to sort the grid exactly as expected. Can you not do the same? I also did this:
    vb.net Code:
    1. Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
    2.     table.DefaultView.Sort = "Thing ASC"
    3. End Sub
    4.  
    5. Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    6.     table.DefaultView.Sort = "Thing DESC"
    7. End Sub
    and those Buttons worked exactly as expected.

  5. #5

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,214

    Re: Sort DataTable or DataView by Custom column

    What happens with my thing, that sounds bad , is that the .ToString method is being called, and not the ICompare interface.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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