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. DataGridView Sorting Problem

DataGridView Sorting Problem

Scheduled Pinned Locked Moved C#
tutorialalgorithmshelp
19 Posts 7 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.
  • A Offline
    A Offline
    AhmedOsamaMoh
    wrote on last edited by
    #1

    Dear ALL Now i have a col . contains for example the following PC1 PC2 PC10 PC100 , if u tried to sort it , it will be PC1 PC10 PC100 PC2 so how to make it sort as it sort numbers to be PC1 PC2 PC10 PC100 my datagridview is binded to a datasource so i can't use SortCompare(object sender, DataGridViewSortCompareEventArgs e) so any tips thanks all bye

    N Richard Andrew x64R G A T 5 Replies Last reply
    0
    • A AhmedOsamaMoh

      Dear ALL Now i have a col . contains for example the following PC1 PC2 PC10 PC100 , if u tried to sort it , it will be PC1 PC10 PC100 PC2 so how to make it sort as it sort numbers to be PC1 PC2 PC10 PC100 my datagridview is binded to a datasource so i can't use SortCompare(object sender, DataGridViewSortCompareEventArgs e) so any tips thanks all bye

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Sort the data before it is bound. You will need to implement a custom IComparer


      I know the language. I've read a book. - _Madmatt

      A 1 Reply Last reply
      0
      • N Not Active

        Sort the data before it is bound. You will need to implement a custom IComparer


        I know the language. I've read a book. - _Madmatt

        A Offline
        A Offline
        AhmedOsamaMoh
        wrote on last edited by
        #3

        any easier solution ??

        N 1 Reply Last reply
        0
        • A AhmedOsamaMoh

          any easier solution ??

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          Yes, wait for .NET Framework 5.0 and hope they have implemented your specific sorting requirements into the DataGridView control.


          I know the language. I've read a book. - _Madmatt

          K 1 Reply Last reply
          0
          • A AhmedOsamaMoh

            Dear ALL Now i have a col . contains for example the following PC1 PC2 PC10 PC100 , if u tried to sort it , it will be PC1 PC10 PC100 PC2 so how to make it sort as it sort numbers to be PC1 PC2 PC10 PC100 my datagridview is binded to a datasource so i can't use SortCompare(object sender, DataGridViewSortCompareEventArgs e) so any tips thanks all bye

            Richard Andrew x64R Offline
            Richard Andrew x64R Offline
            Richard Andrew x64
            wrote on last edited by
            #5

            If you're able to control the format of the data, you can achieve what you want by padding the small numbers with leading zeros: PC001 PC002 PC010 PC100

            The difficult we do right away... ...the impossible takes slightly longer.

            N 1 Reply Last reply
            0
            • A AhmedOsamaMoh

              Dear ALL Now i have a col . contains for example the following PC1 PC2 PC10 PC100 , if u tried to sort it , it will be PC1 PC10 PC100 PC2 so how to make it sort as it sort numbers to be PC1 PC2 PC10 PC100 my datagridview is binded to a datasource so i can't use SortCompare(object sender, DataGridViewSortCompareEventArgs e) so any tips thanks all bye

              G Offline
              G Offline
              GenJerDan
              wrote on last edited by
              #6

              Normalize the numbers? Read the table from a view instead of directly and have a new computed column containing the data as PC001, PC002, PC010, PC100, etc. Sort on that, but don't show it.

              N 1 Reply Last reply
              0
              • G GenJerDan

                Normalize the numbers? Read the table from a view instead of directly and have a new computed column containing the data as PC001, PC002, PC010, PC100, etc. Sort on that, but don't show it.

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #7

                So now you need to create a process in SQL Server to create the column and format the strings so they can be sorted. :rolleyes: What happens when there are more than three digits? What happens when the business logic changes or the field changes?


                I know the language. I've read a book. - _Madmatt

                G 1 Reply Last reply
                0
                • Richard Andrew x64R Richard Andrew x64

                  If you're able to control the format of the data, you can achieve what you want by padding the small numbers with leading zeros: PC001 PC002 PC010 PC100

                  The difficult we do right away... ...the impossible takes slightly longer.

                  N Offline
                  N Offline
                  Not Active
                  wrote on last edited by
                  #8

                  And what if the data needs to be displayed as is without leading zeros?


                  I know the language. I've read a book. - _Madmatt

                  D Richard Andrew x64R 2 Replies Last reply
                  0
                  • N Not Active

                    So now you need to create a process in SQL Server to create the column and format the strings so they can be sorted. :rolleyes: What happens when there are more than three digits? What happens when the business logic changes or the field changes?


                    I know the language. I've read a book. - _Madmatt

                    G Offline
                    G Offline
                    GenJerDan
                    wrote on last edited by
                    #9

                    Mark Nischalke wrote:

                    What happens when there are more than three digits?

                    Um...you take 15 seconds to increase the size in function? Or set it to left-pad with zeros out to 20* places from the get-go and not worry about it? (* or whatever is reasonable)

                    Mark Nischalke wrote:

                    What happens when the business logic changes or the field changes?

                    Well, that changes more than this little bit, so why worry about it since a boatload of stuff will need to be rewritten, anyway? And, if that happened, you'd be rewriting any custom sorting in the program itself if you made your own comparer there. Me, I'd rather avoid that and just change a view.

                    N 1 Reply Last reply
                    0
                    • G GenJerDan

                      Mark Nischalke wrote:

                      What happens when there are more than three digits?

                      Um...you take 15 seconds to increase the size in function? Or set it to left-pad with zeros out to 20* places from the get-go and not worry about it? (* or whatever is reasonable)

                      Mark Nischalke wrote:

                      What happens when the business logic changes or the field changes?

                      Well, that changes more than this little bit, so why worry about it since a boatload of stuff will need to be rewritten, anyway? And, if that happened, you'd be rewriting any custom sorting in the program itself if you made your own comparer there. Me, I'd rather avoid that and just change a view.

                      N Offline
                      N Offline
                      Not Active
                      wrote on last edited by
                      #10

                      GenJerDan wrote:

                      you'd be rewriting any custom sorting in the program itself if you made your own comparer there.

                      Not quite. Had similar requirements and wrote the comparer well enough the first time, no need to keep updating it.


                      I know the language. I've read a book. - _Madmatt

                      1 Reply Last reply
                      0
                      • N Not Active

                        Yes, wait for .NET Framework 5.0 and hope they have implemented your specific sorting requirements into the DataGridView control.


                        I know the language. I've read a book. - _Madmatt

                        K Offline
                        K Offline
                        Keith Barrow
                        wrote on last edited by
                        #11

                        Daoist Coding and Sarcasm, I like it!

                        Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                        -Or-
                        A Dead ringer for Kate Winslett[^]

                        N 1 Reply Last reply
                        0
                        • N Not Active

                          And what if the data needs to be displayed as is without leading zeros?


                          I know the language. I've read a book. - _Madmatt

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

                          You can have a seperate column in the datatable that contains a normalized version of the display data. This normalized version could be used for sorting. Then, when you bind the grid to the table, you simply don't include this sort column in the bindings.

                          A guide to posting questions on CodeProject[^]
                          Dave Kreskowiak

                          N 1 Reply Last reply
                          0
                          • D Dave Kreskowiak

                            You can have a seperate column in the datatable that contains a normalized version of the display data. This normalized version could be used for sorting. Then, when you bind the grid to the table, you simply don't include this sort column in the bindings.

                            A guide to posting questions on CodeProject[^]
                            Dave Kreskowiak

                            N Offline
                            N Offline
                            Not Active
                            wrote on last edited by
                            #13

                            Yes, of course. I was trying to point out to the OP is isn't as simple as just changing the data in the column.


                            I know the language. I've read a book. - _Madmatt

                            D 1 Reply Last reply
                            0
                            • K Keith Barrow

                              Daoist Coding and Sarcasm, I like it!

                              Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                              -Or-
                              A Dead ringer for Kate Winslett[^]

                              N Offline
                              N Offline
                              Not Active
                              wrote on last edited by
                              #14

                              Sarcasm? ;P


                              I know the language. I've read a book. - _Madmatt

                              1 Reply Last reply
                              0
                              • N Not Active

                                And what if the data needs to be displayed as is without leading zeros?


                                I know the language. I've read a book. - _Madmatt

                                Richard Andrew x64R Offline
                                Richard Andrew x64R Offline
                                Richard Andrew x64
                                wrote on last edited by
                                #15

                                He asked for an easier solution and I suggested one. It's up to him to decide whether it works for his particular case. He didn't introduce that additional requirement, you did.

                                The difficult we do right away... ...the impossible takes slightly longer.

                                N 1 Reply Last reply
                                0
                                • Richard Andrew x64R Richard Andrew x64

                                  He asked for an easier solution and I suggested one. It's up to him to decide whether it works for his particular case. He didn't introduce that additional requirement, you did.

                                  The difficult we do right away... ...the impossible takes slightly longer.

                                  N Offline
                                  N Offline
                                  Not Active
                                  wrote on last edited by
                                  #16

                                  An "easy" solution that ignores potential pitfalls is not good advice, but, yes, it is up to the OP.


                                  I know the language. I've read a book. - _Madmatt

                                  1 Reply Last reply
                                  0
                                  • N Not Active

                                    Yes, of course. I was trying to point out to the OP is isn't as simple as just changing the data in the column.


                                    I know the language. I've read a book. - _Madmatt

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

                                    Yeah, some people just don't want to write code to solve problems anymore. They think that the .NET Framework can read minds and do everything you intended with just a few statements.

                                    A guide to posting questions on CodeProject[^]
                                    Dave Kreskowiak

                                    1 Reply Last reply
                                    0
                                    • A AhmedOsamaMoh

                                      Dear ALL Now i have a col . contains for example the following PC1 PC2 PC10 PC100 , if u tried to sort it , it will be PC1 PC10 PC100 PC2 so how to make it sort as it sort numbers to be PC1 PC2 PC10 PC100 my datagridview is binded to a datasource so i can't use SortCompare(object sender, DataGridViewSortCompareEventArgs e) so any tips thanks all bye

                                      A Offline
                                      A Offline
                                      AhmedOsamaMoh
                                      wrote on last edited by
                                      #18

                                      well thx all well i guess the easiest way :omg: that i will recode my code by using the datagridview without the databinding so i can use SortCompare(object sender, DataGridViewSortCompareEventArgs e) thanks anyway all and thanks also to whom comment on my Q in a funny way:confused::confused: bye

                                      1 Reply Last reply
                                      0
                                      • A AhmedOsamaMoh

                                        Dear ALL Now i have a col . contains for example the following PC1 PC2 PC10 PC100 , if u tried to sort it , it will be PC1 PC10 PC100 PC2 so how to make it sort as it sort numbers to be PC1 PC2 PC10 PC100 my datagridview is binded to a datasource so i can't use SortCompare(object sender, DataGridViewSortCompareEventArgs e) so any tips thanks all bye

                                        T Offline
                                        T Offline
                                        tim_gunning
                                        wrote on last edited by
                                        #19

                                        bing bong[^]

                                        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