Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. custom sorting datatables

custom sorting datatables

Scheduled Pinned Locked Moved Visual Basic
databasehelpcsharpalgorithms
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Please note that I am a beginner VB.net programmer. Any help you can offer would be appreciated. I am trying to sort a datatable loaded from an SQL db table. There are 2 sort keys, both ascending, and 9 other columns. The primary key, "PurchaseOrderID", can contain nulls, although it is usually a string of format "999999_999". After loading the ds, I use the following code to try to sort the records: Dim myDataView As DataView myDataView = ds.Tables("tPIA").DefaultView myDataView.Sort = ("PurchaseOrderID, TimeNeeded") The problem is I need the rows with null values in PurchaseOrderID to all sort to the high end. I know one can write a custom comparer to modify sort order but I haven't seen the syntax of how one can use a comparer with a dataview. Can this be done? If so, what's the syntax? If not, do you have any other suggestions? I originally loaded the SQL table into a Collection but there didn't seem to be a sort method available there. So I loaded the Collection into an array and wrote a custom comparer, but then I couldn't seem to sort on the 2 fields, as required. Thank you, mif mif

    O 1 Reply Last reply
    0
    • L Lost User

      Please note that I am a beginner VB.net programmer. Any help you can offer would be appreciated. I am trying to sort a datatable loaded from an SQL db table. There are 2 sort keys, both ascending, and 9 other columns. The primary key, "PurchaseOrderID", can contain nulls, although it is usually a string of format "999999_999". After loading the ds, I use the following code to try to sort the records: Dim myDataView As DataView myDataView = ds.Tables("tPIA").DefaultView myDataView.Sort = ("PurchaseOrderID, TimeNeeded") The problem is I need the rows with null values in PurchaseOrderID to all sort to the high end. I know one can write a custom comparer to modify sort order but I haven't seen the syntax of how one can use a comparer with a dataview. Can this be done? If so, what's the syntax? If not, do you have any other suggestions? I originally loaded the SQL table into a Collection but there didn't seem to be a sort method available there. So I loaded the Collection into an array and wrote a custom comparer, but then I couldn't seem to sort on the 2 fields, as required. Thank you, mif mif

      O Offline
      O Offline
      OICU812
      wrote on last edited by
      #2

      Try this MyDataView.Sort = "PurchaseOrderID, TimeNeeded ASC" -or- MyDataView.Sort = "PurchaseOrderID ASC, TimeNeeded ASC" Use DESC for Descending *EDIT* I just tried it with a dataview containing null values and it sorts the null values to the top of list in my datagrid. I really don't see a problem with your code. I thought maybe the parentheses in your .sort code was the problem but it works either way.

      L 1 Reply Last reply
      0
      • O OICU812

        Try this MyDataView.Sort = "PurchaseOrderID, TimeNeeded ASC" -or- MyDataView.Sort = "PurchaseOrderID ASC, TimeNeeded ASC" Use DESC for Descending *EDIT* I just tried it with a dataview containing null values and it sorts the null values to the top of list in my datagrid. I really don't see a problem with your code. I thought maybe the parentheses in your .sort code was the problem but it works either way.

        L Offline
        L Offline
        LadyReader
        wrote on last edited by
        #3

        Thank you for the help - however I am not getting the results expected. Although ASC is the deafult, I did add ASC to both sort keys: myDataView.Sort = ("PurchaseOrderID ASC, TimeNeeded ASC") Here is a sample of the output, where PurchaseOrderID is column(2) and TimeNeeded is column(9) on a 0 to 11 basis: Row: 119 34028_1_1, 7/25/2005 12:00:00 AM, {unspecified}, , 12345-0003, 1, xxx, 0, , 7/25/2005 12:00:00 AM, M, ------------------------ Row: 120 33734_1_1, 9/11/2005 12:00:00 AM, {unspecified}, , 12346-0002, 1, xxx, 0, , 9/11/2005 12:00:00 AM, M, ------------------------ Row: 121 33597_1_1, 12/12/2012 12:00:00 AM, {unspecified}, , 12345-0001, 1, xxx, 0, , 12/12/2012 12:00:00 AM, M, ------------------------ Row: 122 33596_1_1, 12/12/2012 12:00:00 AM, {unspecified}, , 12345-0001, 1, xxx, 0, , 12/12/2012 12:00:00 AM, M, ------------------------ Row: 123 33596_1_2, 12/12/2012 12:00:00 AM, {unspecified}, , 12345-0001, 2, xxx, 0, , 12/12/2012 12:00:00 AM, M, ------------------------ Row: 124 33597_1_2, 12/12/2012 12:00:00 AM, {unspecified}, , 12344-0001, 2, xxx, 0, , 12/12/2012 12:00:00 AM, M, ------------------------ Row: 125 32798_1_1, 12/31/2029 12:00:00 AM, {unspecified}, , 1234-0003, 2, xxx, 0, , 12/31/2029 12:00:00 AM, M, ------------------------ Row: 126 32412_1_5, 12/31/2029 12:00:00 AM, {unspecified}, , 12345-0002, 3, xxx, 0, , 12/31/2029 12:00:00 AM, M, ------------------------ Row: 127 118121-000, 1/28/2005 12:00:00 AM, {unspecified}, 25, 12345-005, 9, xxx, 0, , 1/28/2005 12:00:00 AM, M, ------------------------ Row: 128 117604-101, 1/13/2005 12:00:00 AM, 147347_020, 540, 12345-1234-006, 1, {unspecified}, 0, 12/22/04, 1/18/2005 12:00:00 AM, P, ------------------------ Row: 129 117749-101, 2/3/2005 12:00:00 AM, 147347_021, 540, 12345-1234-006, 1, {unspecified}, 0, 12/22/04, 2/8/2005 12:00:00 AM, P, ------------------------ Row: 130 117941-101, 3/9/2005 12:00:00 AM, 150236_005, 105, 12345-001, 10500, {unspecified}, 0, 03/07/05, 1/19/2005 12:00:00 AM, M, ------------------------ Row: 131 118122-000, 1/26/2005 12:00:00 AM, {unspecified}, 20, 12345-004, 12, xxxx, 0, , 1/26/2005 12:00:00 AM, M, ------------------------ Row: 132 118110-000, 12/20/2004 12:00:00 AM, {unspecified}, 20, 12345-003, 5, xxxx, 0, , 12/9/2004 12:00:00 AM, M, ------------------------ Take a look at rows 126 and 127 - although PurchaseOrderID has been {unspecified} through this point, TimeNeeded has jumped backwards from 12/31/2029 to 1/28/2005. The

        O 1 Reply Last reply
        0
        • L LadyReader

          Thank you for the help - however I am not getting the results expected. Although ASC is the deafult, I did add ASC to both sort keys: myDataView.Sort = ("PurchaseOrderID ASC, TimeNeeded ASC") Here is a sample of the output, where PurchaseOrderID is column(2) and TimeNeeded is column(9) on a 0 to 11 basis: Row: 119 34028_1_1, 7/25/2005 12:00:00 AM, {unspecified}, , 12345-0003, 1, xxx, 0, , 7/25/2005 12:00:00 AM, M, ------------------------ Row: 120 33734_1_1, 9/11/2005 12:00:00 AM, {unspecified}, , 12346-0002, 1, xxx, 0, , 9/11/2005 12:00:00 AM, M, ------------------------ Row: 121 33597_1_1, 12/12/2012 12:00:00 AM, {unspecified}, , 12345-0001, 1, xxx, 0, , 12/12/2012 12:00:00 AM, M, ------------------------ Row: 122 33596_1_1, 12/12/2012 12:00:00 AM, {unspecified}, , 12345-0001, 1, xxx, 0, , 12/12/2012 12:00:00 AM, M, ------------------------ Row: 123 33596_1_2, 12/12/2012 12:00:00 AM, {unspecified}, , 12345-0001, 2, xxx, 0, , 12/12/2012 12:00:00 AM, M, ------------------------ Row: 124 33597_1_2, 12/12/2012 12:00:00 AM, {unspecified}, , 12344-0001, 2, xxx, 0, , 12/12/2012 12:00:00 AM, M, ------------------------ Row: 125 32798_1_1, 12/31/2029 12:00:00 AM, {unspecified}, , 1234-0003, 2, xxx, 0, , 12/31/2029 12:00:00 AM, M, ------------------------ Row: 126 32412_1_5, 12/31/2029 12:00:00 AM, {unspecified}, , 12345-0002, 3, xxx, 0, , 12/31/2029 12:00:00 AM, M, ------------------------ Row: 127 118121-000, 1/28/2005 12:00:00 AM, {unspecified}, 25, 12345-005, 9, xxx, 0, , 1/28/2005 12:00:00 AM, M, ------------------------ Row: 128 117604-101, 1/13/2005 12:00:00 AM, 147347_020, 540, 12345-1234-006, 1, {unspecified}, 0, 12/22/04, 1/18/2005 12:00:00 AM, P, ------------------------ Row: 129 117749-101, 2/3/2005 12:00:00 AM, 147347_021, 540, 12345-1234-006, 1, {unspecified}, 0, 12/22/04, 2/8/2005 12:00:00 AM, P, ------------------------ Row: 130 117941-101, 3/9/2005 12:00:00 AM, 150236_005, 105, 12345-001, 10500, {unspecified}, 0, 03/07/05, 1/19/2005 12:00:00 AM, M, ------------------------ Row: 131 118122-000, 1/26/2005 12:00:00 AM, {unspecified}, 20, 12345-004, 12, xxxx, 0, , 1/26/2005 12:00:00 AM, M, ------------------------ Row: 132 118110-000, 12/20/2004 12:00:00 AM, {unspecified}, 20, 12345-003, 5, xxxx, 0, , 12/9/2004 12:00:00 AM, M, ------------------------ Take a look at rows 126 and 127 - although PurchaseOrderID has been {unspecified} through this point, TimeNeeded has jumped backwards from 12/31/2029 to 1/28/2005. The

          O Offline
          O Offline
          OICU812
          wrote on last edited by
          #4

          mfriedenthal wrote: The watch has these colums delimited by "#"s so I have reason to believe that .net is indeed seeing the fields as dates and not strings. Are you sure the data isn't being handled as a string? I made a quick table in ms access and it sorted it fine with the "TimeNeeded" column as a DATE/TIME but when i changed it to a string it basically returned results like you show. Double check the column data type: Debug.WriteLine(MyDataSet.Tables("MyTable").Columns("TimeNeeded").DataType.ToString) Do the same for your PurchaseOrderID column. Other than being the wrong datatype I can't understand why it doesn't work for you.

          L 1 Reply Last reply
          0
          • O OICU812

            mfriedenthal wrote: The watch has these colums delimited by "#"s so I have reason to believe that .net is indeed seeing the fields as dates and not strings. Are you sure the data isn't being handled as a string? I made a quick table in ms access and it sorted it fine with the "TimeNeeded" column as a DATE/TIME but when i changed it to a string it basically returned results like you show. Double check the column data type: Debug.WriteLine(MyDataSet.Tables("MyTable").Columns("TimeNeeded").DataType.ToString) Do the same for your PurchaseOrderID column. Other than being the wrong datatype I can't understand why it doesn't work for you.

            L Offline
            L Offline
            LadyReader
            wrote on last edited by
            #5

            Thanks for the advice, but I did as you suggested and I got back: System.DateTime System.String for TimeNeeded and PurchaseOrderID, respectively. Which is what I expected. Still researching... Any help would be appreciated. Thanks!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups