Sort top X rows of DataGridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • frenzy99
    New Member
    • Nov 2008
    • 4

    Sort top X rows of DataGridView

    Can any one tell me how to tweak the existing Sort method, in DataGridView Class, so as I can sort only certain top rows.

    Say 5 rows.
    And i want to sort only first 3 rows. Last 2 should be left, as such.

    example: (row number followed by string)
    1.C
    2.A
    3.B
    4.E
    5.D

    the expected output:
    1.A
    2.B
    3.C
    4.E
    5.D

    Please help me, as I'm new to .Net
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    I believe that you could do this via sql and have an exclusion criteria in your where clause. This may depend on the nature of your data, as you will still be effectively sorting the column just excluding the values in the other rows..

    Comment

    • nukefusion
      Recognized Expert New Member
      • Mar 2008
      • 221

      #3
      Is your grid bound to a datasource? If so then you should perform the sort on the underlying datasource.
      If not, you could indeed use the DataGridView.So rt() method. You would need to create a class that implements the IComparer interface. This interface has one method - Compare. Within that method you would compare two objects and return a result that specifies whether object A is less than, equal to, or greater than object B.
      If you're only trying to sort A's B's and C's then this might be feasible, however, if you want to sort a large range of values you're custom sorting code might become quite substantial.
      How large is the range of expected values in your column?

      Comment

      Working...