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. C#
  4. how sort datgridview or listview like windows explorer

how sort datgridview or listview like windows explorer

Scheduled Pinned Locked Moved C#
help
9 Posts 5 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
    Le rner
    wrote on last edited by
    #1

    hi all, i have a datagrid and listview with some items. like,counting from 1 to 20 than when i sort its shows 1,10,11 and so on i want it sort like 1,2,3 as acending or descending selected plz help me for this. thanks in advance.

    OriginalGriffO Richard DeemingR R 3 Replies Last reply
    0
    • L Le rner

      hi all, i have a datagrid and listview with some items. like,counting from 1 to 20 than when i sort its shows 1,10,11 and so on i want it sort like 1,2,3 as acending or descending selected plz help me for this. thanks in advance.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      That's the correct sorting, for your data - the problem is deeper than that and probably requires changes elsewhere. When you sort numbers, you expect this order: 1, 2, 3, ... 9, 10, 11, ... 19, 20 ... But when you sort strings, the whole comparison is based on the first different pair of characters you encounter in the two strings. So if you compare "APPLE" with "APART"it does this: 1) Compare index [0]: 'A' and 'A'. Same. Continue 2) Compare index [1]: 'P' and 'P'. Same. Continue 3) Compare index [2]: 'P' and 'A'. Different: Since 'A' is less than 'P' return the second string as the "lowest" value. Indexes [3] and above aren't even looked at. That's all fine and dandy, except if you are comparing string values and expecting numeric ordering, because the string ordering is used regardless of the string content and the order goes: 1, 10, 11, ... 19, 2, 20, 21, ... 29, 3, 30, ... This problem gets even worse with date based string data! So what you actually need to do is look to the source of your data and find out why it is a string rather than a numeric value, and correct that. Often, it's due to bad design in a DB where all the data is stored as strings because that was easy to do and caused no errors when bad numbers or bad dates where INSERTed - but the best way to fix this is to fix the DB design so you store numbers in numeric fields, and dates in date based fields.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      L 1 Reply Last reply
      0
      • L Le rner

        hi all, i have a datagrid and listview with some items. like,counting from 1 to 20 than when i sort its shows 1,10,11 and so on i want it sort like 1,2,3 as acending or descending selected plz help me for this. thanks in advance.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        If your data contains a mixture of numbers and strings and you want to precisely match the way Windows Explorer sorts file names, then you can either P/Invoke the StrCmpLogicalW function, or use a managed implementation: Numeric String Sort in C#[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        L 2 Replies Last reply
        0
        • OriginalGriffO OriginalGriff

          That's the correct sorting, for your data - the problem is deeper than that and probably requires changes elsewhere. When you sort numbers, you expect this order: 1, 2, 3, ... 9, 10, 11, ... 19, 20 ... But when you sort strings, the whole comparison is based on the first different pair of characters you encounter in the two strings. So if you compare "APPLE" with "APART"it does this: 1) Compare index [0]: 'A' and 'A'. Same. Continue 2) Compare index [1]: 'P' and 'P'. Same. Continue 3) Compare index [2]: 'P' and 'A'. Different: Since 'A' is less than 'P' return the second string as the "lowest" value. Indexes [3] and above aren't even looked at. That's all fine and dandy, except if you are comparing string values and expecting numeric ordering, because the string ordering is used regardless of the string content and the order goes: 1, 10, 11, ... 19, 2, 20, 21, ... 29, 3, 30, ... This problem gets even worse with date based string data! So what you actually need to do is look to the source of your data and find out why it is a string rather than a numeric value, and correct that. Often, it's due to bad design in a DB where all the data is stored as strings because that was easy to do and caused no errors when bad numbers or bad dates where INSERTed - but the best way to fix this is to fix the DB design so you store numbers in numeric fields, and dates in date based fields.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

          L Offline
          L Offline
          Le rner
          wrote on last edited by
          #4

          yes i want to compare string items

          D 1 Reply Last reply
          0
          • L Le rner

            yes i want to compare string items

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            If you're trying to sort numeric items as strings, you're not going to get the result you want. The column that you want to sort in 1, 2, 3, ... order MUST be numeric in order for sorting to work the way you want.

            Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
            Dave Kreskowiak

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              If your data contains a mixture of numbers and strings and you want to precisely match the way Windows Explorer sorts file names, then you can either P/Invoke the StrCmpLogicalW function, or use a managed implementation: Numeric String Sort in C#[^]


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              L Offline
              L Offline
              Le rner
              wrote on last edited by
              #6

              thanks this really helps me...

              1 Reply Last reply
              0
              • L Le rner

                hi all, i have a datagrid and listview with some items. like,counting from 1 to 20 than when i sort its shows 1,10,11 and so on i want it sort like 1,2,3 as acending or descending selected plz help me for this. thanks in advance.

                R Offline
                R Offline
                RobertSF
                wrote on last edited by
                #7

                One solution is to create a class for a "sortable" list and use that as your data source in the DGV. I found this somewhere and have been using it with success. Thanks to the unsung hero who wrote it.

                using System;
                using System.Collections.Generic;
                using System.ComponentModel;
                using System.Linq;
                using System.Runtime.InteropServices;

                namespace YourNamespace
                {
                internal static class NativeMethods
                {
                [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
                internal static extern int StrCmpLogicalW(string aString, string bString);
                }

                internal class SortableList : BindingList
                {
                    protected override bool SupportsSortingCore
                    {
                        get
                        {
                            return true;
                        }
                    }
                
                    protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
                    {
                        int modifier = direction == ListSortDirection.Ascending ? 1 : -1;
                        if (prop?.PropertyType.GetInterface("IComparable") != null)
                        {
                            var items = Items.ToList() as List;
                            items.Sort(new Comparison((a, b) =>
                            {
                                var aVal = prop.GetValue(a).ToString() as string;
                                var bVal = prop.GetValue(b).ToString() as string;
                                return NativeMethods.StrCmpLogicalW(aVal, bVal) \* modifier;
                            }));
                            Items.Clear();
                            foreach (var i in items) Items.Add(i);
                        }
                    }
                }
                

                }

                1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  If your data contains a mixture of numbers and strings and you want to precisely match the way Windows Explorer sorts file names, then you can either P/Invoke the StrCmpLogicalW function, or use a managed implementation: Numeric String Sort in C#[^]


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  L Offline
                  L Offline
                  Le rner
                  wrote on last edited by
                  #8

                  for string its working fine, now what about date time, how i compare here date time of file, to sort according this

                  Richard DeemingR 1 Reply Last reply
                  0
                  • L Le rner

                    for string its working fine, now what about date time, how i compare here date time of file, to sort according this

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #9

                    Don't use a string column; use a DateTime column, and apply the format in the UI. If you really want to sort dates stored as strings, then you have to store them in a sortable format - eg: yyyy-MM-dd HH:mm:ss.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    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